2012-09-25 2 views
0

어제이 코드의 다른 부분을 게시했지만 다른 문제가 발생했습니다. RPG 용 캐릭터 생성기를 만들었고 .txt 파일에 문자 시트 함수의 출력을 얻으려고 시도했지만, 무슨 일이 일어 났는지는 함수가 통계의 일부에 대해 None 값을 반환 할 수 있다고 생각합니다. 완전히 정상적입니다.) 그리고 .txt 파일에 쓰려고 할 때 오류가 발생합니다. 나는 완전히 엉망이되어 도움을 크게 받았다.대부분이 파일에 인쇄하는이 함수를 어떻게 작성합니까?

# Character Sheet Function. 
def char_shee(): 
    print "Name:", name 
    print "Class:", character_class 
    print "Class Powers:", class_power 
    print "Alignment:", alignment 
    print "Power:", pow, pow_mod() 
    print "Intelligence:", iq, iq_mod() 
    print "Agility:", agi, agi_mod() 
    print "Constitution:", con, con_mod() 
    print "Cynicism:", cyn, cyn_mod() 
    print "Charisma:", cha, cha_mod() 
    print "All Characters Start With 3 Hit Dice" 
    print""" 
\t\t{0}'s History 
\t\t------------------ 
\t\tAge:{1} 
\t\t{2} 
\t\t{3} 
\t\t{4} 
\t\t{5} 
\t\t{6} 
\t\t{7} 
\t\t{8} 
\t\t{9} 
\t\tGeneral Disposition: {10} 
\t\tMost important thing is: {11} 
\t\tWho is to blame for worlds problems: {12} 
\t\tHow to solve the worlds problems: {13} 
""".format(name, age, gender_id, ethnic_pr, fcd, wg, fogo_fuck, cur_fam,fam_fuk, nat_nur, gen_dis, wha_wor, who_pro, how_pro) 

char_shee() 
print "Press enter to continue" 
raw_input() 

# Export to text file? 
print """Just because I like you, let me know if you want this character 
saved to a text file. Please remember if you save your character not to 
name it after something important, or you might lose it. 
""" 
text_file = raw_input("Please type 'y' or 'n', if you want a .txt file") 
if text_file == "y": 
    filename = raw_input("\nWhat are we calling your file, include .txt") 
    target = open(filename, 'w') 
    target.write(char_shee() 
    target.close 
    print "\nOk I created your file." 
    print """ 
Thanks so much for using the Cyberpanky N.O.W Character Generator 
By Ray Weiss 
Goodbye 
""" 
else: 
    print """ 
Thanks so much for using the Cyberpanky N.O.W Character Generator 
By Ray Weiss 
Goodbye 
""" 

편집 : 다음은 출력 내가 얻을 :

> Please type 'y' or 'n', if you want a .txt filey 
> 
> What are we calling your file, include .txt123.txt <function char_shee 
> at 0x2ba470> Traceback (most recent call last): File "cncg.py", line 
> 595, in <module> 
>  target.write(pprint(char_shee)) TypeError: must be string or read-only character buffer, not None 
+0

실제 오류 추적을 제시해주십시오. – geoffspear

+0

또한'target.close'는 아무 것도하지 않습니다. 'target.close()'는 무언가를합니다. – mgilson

+0

그냥 충고 만 해보자. 포맷 할 항목이 두 개 이상 있다면 '... {blah} {foo} {bar} ...'형식을 사용하라. (blah = ..., foo = ... , bar = ...)'구문을 사용하면 나중에 감사 할 것입니다. –

답변

1

당신이 여기 괄호 잊었 :

target.write(char_shee()) 
target.close() 

및 @Martijn 피에 터스가 지적한대로 char_shee()에서 값을 반환하는 대신한다 그 (것)들을 인쇄하십시오.

+1

'char_shee()'는 실제로 아무 것도 반환하지 않기 때문에 도움이되지 않습니다. 그냥 인쇄됩니다. – geoffspear

+1

@Woobie - 글쎄, SyntaxError를 없애기 위해 * 도움이 될 것이다. 그것은이 코드에서 최소한 2 가지 문제 중 하나를 수정합니다 ... Martijn의 대답은 다른 것을 해결합니다. – mgilson

3

print을 쓰면 이 반환되지 않습니다. 값이 반환됩니다.

char_shee에 문자 시트 문자열을 반환하여 파일에 쓰려면, 대신 해당 문자열을 작성해야합니다. 문자열을 구축 용이성

, 당신의 문자열을 수집 목록을 사용

def char_shee(): 
    sheet = [] 
    sheet.append("Name: " + name) 
    sheet.append("Class: " + character_class) 
    # ... more appends ... 

    # Return the string with newlines 
    return '\n'.join(sheet) 
+0

인쇄 대신 반환 사용 하시겠습니까? 나는 내가 할 수 있다고 생각한다. 고마워! – lerugray

+0

임, 지금 그것을 함께 넣어, 얼마나 나쁜지에 관해서는 나쁜 업데이트, 감사합니다! – lerugray

+0

@lerugray : 새로운 문제가 생기면 새로운 질문을 게시하십시오. 이 질문의 전제를 바꾸지 마십시오. –

관련 문제