所以我正在尝试创建一个CSV文件并允许它以特定方式读取并导回到程序中。它应该按字母顺序或数字,从最高到最低。 代码如下。已导入随机,时间和CSV。请忽略第2和第3类,因为它们尚未正确格式化。那么我怎么能让它从文件中取回来组织它?
import random
import time
import csv
Score = 0
CorrectAnswer = 0
Op = ('X', '+', '-')
if CorrectAnswer == 0:
print("Hello! Welcome to the maths quiz. You will be asked 10 questions. They will be X, + or -.")
time.sleep (2)
Name = input(str("Before we start can you please put in your name?"))
Class = int(input('Also Which class are you in? 1, 2 or 3'))
print("Hello, get ready "+Name)
for loop in range(1):
Number_1 = random.randint(1,10)
Number_2 = random.randint(1,10)
MathsOp = str(random.choice(Op))
print (Number_1, MathsOp, Number_2)
UserAnswer = input('So what is your answer?')
if MathsOp == 'X':
CorrectAnswer = (CorrectAnswer + Number_1 * Number_2)
print (CorrectAnswer)
if MathsOp == '-':
CorrectAnswer = (CorrectAnswer + Number_1 - Number_2)
print (CorrectAnswer)
if MathsOp == '+':
CorrectAnswer = (CorrectAnswer + Number_1 + Number_2)
print (CorrectAnswer)
if int(UserAnswer) == CorrectAnswer:
print ('Well Done! Next Question!')
Score = (Score + 1)
CorrectAnswer = 0
else:
print ('Unlucky, better luck next time, Next Question!')
CorrectAnswer = 0
print ('Well done', Name, '. You got ', Score)
if Class == 1:
PupilList = [(Name),(Score)]
file = open('Class_1.csv', 'w')
a = csv.writer(PupilList, delimiter='|', lineterminator='\n')
a.writerows(PupilList)
file.close()
if Class == 2:
file = open('Class_2.csv', 'a')
file.write((Name) + '|' + str(Score) + '\n')
file.write('')
file.close()
if Class == 3:
file=open('Class_3.csv', 'a')
file.write((Name) + '|' + str(Score) + '\n')
file.write('')
file.close()