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. If you change that to 'r+'
the problem may solve itself. Without an actual traceback there's no way to know for sure.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.