编码时我遇到了这个错误,我不知道这意味着什么。
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())
答案 0 :(得分:1)
class_name
是int
。你在这里分配:
class_name=int(input("Are you in class 1, 2 or 3? "))
但是,您无法连接整数和字符串,因此必须先将int
转换为str
:
filename= str(class_name) + ".csv"