-3
我在编码时遇到了这个错误,我不知道它的含义。为什么我会得到“不受支持的操作数类型错误?
line 59, in <module>
filename=class_name + ".csv"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
我不知道我在哪弄乱了,但它可能是与类号验证。
while True:
try:
class_name=int(input("Are you in class 1, 2 or 3? "))
if class_name in [1, 2, 3]:
print(name,", welcome to this maths test!")
break
else:
print ("Class has to be 1, 2 or 3")
except ValueError:
print ("Class has to be 1, 2 or 3")
filename=class_name + ".csv"
with open(filename, 'a') as f:
f.write(str(name) + " : " + str(score) + '\n')
if results("Do you wish to view previous results for your class? (yes or no) "):
with open(filename, 'r') as f:
print(f.read())
所以,你正确的“中投”'CLASS_NAME '输入到'int',所以现在它是一个'int'类型。 '“.csv”'是一个s'str'类型。你可以是'filename = str(class_name)+“.csv”'或'filename =“{} .csv”.format(class_name)'' – dwanderson