我的问题可能和平时的男人和女孩一样简单,我现在已经做了13个小时的工作了,我无法通过这个来积累名字变量。
我需要它来统计我输入数据的玩家。这是我得到的错误消息:
line 25, in main
name += 1
TypeError: Can't convert 'int' object to str implicitly
对于此输入/输出:
You will be asked to enter players names and scores
When you have no more names, enter End
Enter Player's Name or end to end program:vxzcvzvc
Enter vxzcvzvc's golf score: 3
Enter Player's Name or end to end program:zcvxcxzvxzv5
Enter zcvxcxzvxzv5's golf score: 6
Enter Player's Name or end to end program:zcvxvczx
Enter zcvxvczx's golf score: 5
Enter Player's Name or end to end program:end
You have written end players records to golf.txt
这是我的代码:
def main():
#Program that reads each player's name and golf score as input
#Save to golf.txt
outfile = open('golf.txt', 'w')
#define name
name = 0
name += 1
# introduction to user explaining required information to be entered
print("You will be asked to enter players names and scores")
print("When you have no more names, enter End")
print("\n")
#Enter input, leave blank to quit program
while True:
name = input("Enter Player's Name or end to end program:")
if name == "end":
break
score = input("Enter "+ str(name)+"'s golf score: ")
#write to file golf.txt
outfile.write(name + "\n")
outfile.write(str(score) + "\n")
#output for number of names recorded
print("You have written", name,"players records to golf.txt")
outfile.close()
main()
答案 0 :(得分:1)
您需要一个单独的变量来计算已写入的分数:
ls