[英]Traceback error when handling text files
I have created this code, which performs a quiz and then writes the result to a text file based on a user input (class). 我创建了此代码,该代码执行测验,然后根据用户输入(类)将结果写入文本文件。 All the file handling code seems sound to me, but when I run the program, I experience a "Traceback (most recent call last): Traceback (most recent call last):" error, and then the program crashes out.
所有文件处理代码对我来说似乎都是正确的,但是当我运行该程序时,我遇到了“跟踪(最近一次调用最近):跟踪(最近一次调用最近):”错误,然后程序崩溃了。
I just had the same problem with a different bit of code and got an answer, but I have not made the error I made again (trying to write multiple variables to a file at once), so that answer isn't applicable this time. 我只是用不同的代码遇到了同样的问题,但是得到了答案,但是我没有再犯我的错误(试图一次将多个变量写入文件),所以这次的答案不适用。
import random
forename=""
surname=""
classno=0
numberone=0
numbertwo=0
correct=False
score=0
ops = ["+", "x", "-"]
while forename == "" or forename.isnumeric():
forename=input("What is your first name? ")
if forename == "":
print("You have to enter your first name.")
if forename.isnumeric() == True:
print("Your name must contain letters.")
while surname == "" or surname.isnumeric():
surname=input("What is your surname? ")
if surname == "":
print("You have to enter your name.")
if surname.isnumeric() == True:
print("Your name must contain letters.")
while classno not in [1,2,3]:
while True:
try:
classno=int(input("What class are you in? "))
break
except ValueError:
print("That wasn't right. Please try again.")
for x in range(10):
operation=random.choice(ops)
if operation == "-":
numberone=random.randint(0,10)
numbertwo=random.randint(0,numberone)
elif operation == "x":
numberone=random.randint(0,12)
numbertwo=random.randint(0,12)
else:
numberone=random.randint(0,100)
numbertwo=random.randint(0,(100-numberone))
while True:
try:
answer=int(input("What is " + str(numberone) + str(operation) + str(numbertwo) + "? "))
break
except ValueError:
print("Incorrect input. Please try again.")
if operation=="+":
if answer==numberone+numbertwo:
correct=True
elif operation=="-":
if answer==numberone-numbertwo:
correct=True
else:
if answer==numberone*numbertwo:
correct=True
if correct==True:
print("Correct!")
score=score+1
else:
print("Wrong!")
correct = False
info = str(forename) + "," + str(surname) + "," + str(score) + "\n"
if classno == 1:
file=open("class1.txt", "a")
elif classno == 2:
file=open("class2.txt", "a")
else:
file=open("class3.txt", "a")
maxnames = sum(1 for line in file)
name = [[] for i in range(maxnames)] #creates empty list
for count in range(maxnames):
line = file.readline() #defines one line
line=line.strip("\n") #defines where lines end
data = line.split(",") #defines each data particle
name[count].append(data[0]) #puts name in
name[count].append(data[1]) #puts scores in
name[count].append(data[2])
name[count].append(data[3])
if forename == name[c][0] and surname == name[c][1]:
name[c][4]=name[c][3]
name[c][3]=name[c][2]
name[c][2]=score
else:
file.write(info)
file.close()
print("You scored",score,"out of 10.")
Here's what I'm seeing: 这是我所看到的:
The problem begins with the fact that you're opening a file for writing (by using the 'a'
mode) and then trying to read from it. 问题始于以下事实:您正在打开一个文件进行写入(通过使用
'a'
模式),然后尝试从中读取文件。 If you change that to 'r+'
the problem may solve itself. 如果将其更改为
'r+'
则问题可能会自行解决。 Without an actual traceback there's no way to know for sure. 没有实际的追溯,就无法确定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.