我一直在尝试将我的属性写入这样的文件:
def player1():
name1=raw_input("what would you like to call the first player?")
diceroll()
print name1,"has:"
time.sleep(1)
z = strength()
time.sleep(1)
x = skill()
f = open('attributes.txt','w+')
f.write(name1)
f.write("has")
f.write(x)
f.write("strength and")
f.write(z)
f.write("skill")
然而,当我开始运行我的代码时,它会出现:
Traceback (most recent call last):
File "H:GCSEComputingProgrammingshorterversion.py", line 83, in <module>
loop()
File "H:GCSEComputingProgrammingshorterversion.py", line 77, in loop
player1()
File "H:GCSEComputingProgrammingshorterversion.py", line 37, in player1
f.write(x)
TypeError: expected a character buffer object
我该如何解决这个问题?这是我的全部代码:
import random
import time
def loop():
global lol
def intro():
print "welcome to an attribute simulator game!"
time.sleep(1)
print "Today we will be calculating the strength and skill of virtual characters!"
def strength():
dice12 = random.randint(1,12)
dice04 = random.randint(1,4)
strfinalstats = dice12/dice04+10
print ">",strfinalstats,"strength"
def skill():
dice12 = random.randint(1,12)
dice04 = random.randint(1,4)
skfinalstats = dice12/dice04+10
print ">",skfinalstats,"skill!"
def diceroll():
print ("The dice of fate are being rolled!!")
def player1():
name1=raw_input("what would you like to call the first player?")
diceroll()
print name1,"has:"
time.sleep(1)
z = strength()
time.sleep(1)
x = skill()
f = open('attributes.txt','w+')
f.write(name1)
f.write("has")
f.write(x)
f.write("strength and")
f.write(z)
f.write("skill")
def player2():
name2=raw_input("what would you like to call the first player?")
diceroll()
print name2,"has:"
time.sleep(1)
strength()
time.sleep(1)
skill()
f = open('attributes.txt','w+')
f.write(name2)
f.write("has")
f.write(x)
f.write("strength and")
f.write(z)
f.write("skill")
def player3():
name3=raw_input("what would you like to call the first player?")
diceroll()
print name3,"has:"
time.sleep(1)
strength()
time.sleep(1)
skill()
f = open('attributes.txt','w+')
f.write(name3)
f.write("has")
f.write(x)
f.write("strength and")
f.write(z)
f.write("skill")
intro()
time.sleep(1)
player1()
player2()
player3()
lol=raw_input("would you like to run the program again? Say yes or press any letter/number key to exit!")
repeat = True
while repeat==True:
loop()
time.sleep(1)
if lol=="yes":
print "ok lets start again!"
repeat = True
else:
print "thankyou for playing!"
time.sleep(1)
print "exiting..."
exit()
您需要考虑打印和返回之间的区别
def skill():
dice12 = random.randint(1,12)
dice04 = random.randint(1,4)
skfinalstats = dice12/dice04+10
print ">",skfinalstats,"skill!"
不显式返回任何内容,这与返回None
相同。
如果您试图将None
写入一个文件,则收到的错误正是您所得到的:
>>> f = open('test.txt', 'wt')
>>> f.write(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected a character buffer object
>>>
将skill()函数更改为类似的函数
def skill():
dice12 = random.randint(1,12)
dice04 = random.randint(1,4)
skfinalstats = dice12/dice04+10
return "> {} skill!".format(skfinalstats)
Python需要一个字符串对象,但是您提供的是strength()
函数的输出。由于您的函数缺少return
语句,因此这将是NoneType
您需要修改您的函数以获得return
语句,我怀疑您希望它看起来像这样:
def strength():
dice12 = random.randint(1,12)
dice04 = random.randint(1,4)
strfinalstats = dice12/dice04+10
print ">",strfinalstats,"strength"
return strfinalstats
顺便说一句,当您写入文件时,您可以使用str.format()
函数使其变得更好:
f.write("{} has {} strength and {} skill".format(name1, x, y))