好吧,我正在尝试将三个原始输入变量保存到文本文件中。一切都运行正常,直到它将文本文件中的信息附加(我认为这就是所谓的)。 它下到列表中的第二个变量,即年龄,然后输出此错误:
Traceback (most recent call last):
File "C:\Users\Owner\Desktop\Management CMS\employee-management.py", line 7, in <module>
fileObj.write(age)
TypeError: expected a character buffer object
我的代码是:
name = raw_input("What is your name?")
age = int(raw_input("How old are you?"))
favcolor = raw_input("What is your favorite color?")
fileObj = open("employee.txt","w")
fileObj.write(name)
fileObj.write(age)
fileObj.write(favcolor)
fileObj.close()
print "The following text has been saved:"
print name
print age
print favcolor
答案 0 :(得分:1)
缓冲区可能需要一个字符串。它应该是:
age = raw_input("How old are you?")
你仍然可以将它作为一个整数强制转换,以确保在将值写入缓冲区之前它是一个int,但是如果python无法转换它,它将引发错误。