Python 如何编写这个主要打印到文件中的函数?
所以我昨天发布了这个代码的另一部分,但我遇到了另一个问题。我为一个RPG制作了一个字符生成器,我试图让程序将一个字符表函数的输出输出到一个.txt文件中,但我想发生的是,该函数可能会为一些统计返回一个Python 如何编写这个主要打印到文件中的函数?,python,python-2.7,Python,Python 2.7,所以我昨天发布了这个代码的另一部分,但我遇到了另一个问题。我为一个RPG制作了一个字符生成器,我试图让程序将一个字符表函数的输出输出到一个.txt文件中,但我想发生的是,该函数可能会为一些统计返回一个None值(这是完全正常的),然后当我尝试写入一个.txt文件时,我会因为这个而得到一个错误。我完全被难倒了,我将非常感谢你的帮助 # Character Sheet Function. def char_shee(): print "Name:", name print "Clas
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
>如果需要.txt文件,请键入“y”或“n”
>
>我们如何调用您的文件,0x2ba470>回溯处的include.txt123.txt(最后一次调用):文件“cncg.py”,第行
>595,在
>target.write(pprint(char_shee))类型错误:必须是字符串或只读字符缓冲区,而不是无
您忘记了此处的括号:
target.write(char_shee())
target.close()
正如@Martijn Pieters指出的,您应该从char_shee()返回值,而不是打印它们。您忘记了这里的括号:
target.write(char_shee())
target.close()
正如@Martijn Pieters指出的,您应该从
char_shee()
返回值,而不是打印它们。使用print
写入sys.stdout
,它不会返回值
如果希望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)
使用
print
写入sys.stdout
,它不会返回值
如果希望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)
请显示实际的错误回溯。还要注意,
target.close
不执行任何操作target.close()
做了一些事情。只是一个建议:如果要格式化的项目多于两个,请使用“…{blah}{foo}{bar}…”格式(blah=…,foo=…,bar=…)
语法,稍后你会感谢你自己。直到我们完成为止:关闭目标的括号。在代码中写入。请显示实际的错误回溯。还要注意目标。关闭不起任何作用target.close()
做了些什么。只是一个建议:如果你有多个项目需要格式化,请使用“…{blah}{foo}{bar}…”格式(blah=…,foo=…,bar=…)
语法,你以后会感谢你自己的。直到我们找到它为止:关闭目标的括号。在你的代码中写入
。不会有帮助,因为char shee()
实际上不返回任何内容,它只是打印。@Woobie——好吧,这将有助于消除Syntaxer错误。它修复了此代码中至少2个问题中的一个。。。Martijn的答案解决了另一个问题。没有帮助,因为char_-shee()
实际上没有返回任何东西,它只是打印出来。@Woobie--好吧,这将有助于消除语法错误。它修复了此代码中至少2个问题中的一个。。。Martijn的回答是针对另一个问题的。使用返回而不是打印?我想我能做到,谢谢!我现在正在整理,我会更新的,谢谢@勒鲁格雷:如果你有新问题,就发一个新问题。不要改变这个问题的前提。你明白了,我想我已经明白了:)再次感谢大家。我会发布一个新的问题,仍然不起作用,新的错误。用return代替print?我想我能做到,谢谢!我现在正在整理,我会更新的,谢谢@勒鲁格雷:如果你有新问题,就发一个新问题。不要改变这个问题的前提。你明白了,我想我已经明白了:)再次感谢大家。我会发布一个新的问题,仍然不起作用,新的错误。