Python 如何对.txt文件进行排序
我做了一个算术测验,问你十个问题和你所在的班级。用户将输入该名称,然后输入1、2或3。然后它会问用户十个问题,但最后它会将用户的姓名和分数保存到一个名为ClassScore、Class2Score或Class3Score的.txt文件中 下面是我的代码,显示算术测验:Python 如何对.txt文件进行排序,python,sorting,format,Python,Sorting,Format,我做了一个算术测验,问你十个问题和你所在的班级。用户将输入该名称,然后输入1、2或3。然后它会问用户十个问题,但最后它会将用户的姓名和分数保存到一个名为ClassScore、Class2Score或Class3Score的.txt文件中 下面是我的代码,显示算术测验: import time import math import random print("Title:Arithmetic Quiz") print("**************************************
import time
import math
import random
print("Title:Arithmetic Quiz")
print("*************************************************************")
print("This program will ask you to complete the arithmetic quiz.")
print("The program has 10 questions. You will recieve feedback after.")
print("____________________________________________________________"
while True:
UserName = input("What is your name?:")
if not UserName.isalpha():
print("Error!Please enter your name using letters. ")
continue
else:
break
ClassSelection= input("Please enter what Class you are in?:1, 2 or 3")
ClassChosen=0
while ClassChosen==0:
if ClassSelection=="1":
ClassChosen=1
elif ClassSelection=="2":
ClassChosen=1
elif ClassSelection=="3":
ClassChosen=1
else:
print("You must write 1, 2, or 3.")
ClassSelection=input("Enter the class you are in")
print(UserName," welcome to the Arithmetic Quiz.")
print("____________________________________________")
print("The quiz will begin in 3 seconds")
time.sleep(2)
for i in range(0,3):
print (3 - i)
time.sleep(1)
print("Begin!")
print("*****************************************")
#___________________________________________________________________________________________________________________________________
RecentStudent= [0,0,0]
def MathsQuiz():
score=0
for questionNum in range(10):
Num1= random.randint (1, 10)
Num2= random.randint (1, 10)
Symbol = ["+","-","*"]
Operation = random.choice(Symbol)
RealAnswer= int(eval(str(Num1)+Operation+str(Num2)))
print("Please give an answer for:", Num1, Operation, Num2)
UserAnswer = int(input("Enter your answer here:"))
if UserAnswer == RealAnswer:
score = score + 1
print("You are correct! :D")
print("_______________________________________________")
else:
print("You are incorrect! :( ")
print("The answer was", RealAnswer)
print("________________________________________________")
print()
print("__________________________________________________")
print("Thank you for completing the quiz!")
print("Your Score is loading")
import time
time.sleep(2)
print(UserName,"In this test you achieved",score,"/10")
print()
del RecentStudent[0]
RecentStudent.append(score)
print("Your three most recent scores are:",RecentStudent)
print("********************************************************")
def Resit1():
Resit1=input("Do you want to resit the test? Yes or No?:")
if Resit1== "Yes" or Resit1=="yes":
MathsQuiz()
def Resit2():
Resit2=input("Do you want to resit the test? Yes or No?:")
if Resit2== "Yes" or Resit2=="yes":
MathsQuiz()
print("Quiz Finished")
#____________________________________________________________________________________________________________________________________________________________
MathsQuiz()
Resit1()
Resit2()
if ClassSelection=="1":
Class1 = []
Class1.append("Student: ")
Class1.append(UserName)
Class1.append("Latest 3 Scores: ")
Class1.append(RecentStudent)
file = open("Class1Scores.txt", 'a')
file.write(str(Class1))
file.write("\n")
file.close()
elif ClassSelection=="2":
Class2=[]
Class2.append("Student: ")
Class2.append(UserName)
Class2.append("Latest 3 Scores: ")
Class2.append(RecentStudent)
file = open("Class2Scores.txt", 'a')
file.write(str(Class2))
file.write("\n")
file.close()
elif ClassSelection==3:
Class3 = []
Class3.append("Student: ")
Class3.append(UserName)
Class3.append("Latest 3 Scores: ")
Class3.append(RecentStudent)
file = open("Class3Scores.txt", 'a')
file.write(str(Class3))
file.write("\n")
file.close()
分数保存在文件中后,我想做的是询问用户他们希望看到什么数据。用户可以看到的数据是最高的结果、最低的结果、平均分数、按字母顺序排列的名称以及每个类的最高分数。我使用虚拟值将用户的最后三个分数以1,2,10的格式保存到文件中
下面是我将分数保存到.txt文件时输出到该文件的示例:
['Student: ', 'clive', 'Latest 3 Scores: ', [0, 0, 0]]
这是它保存的格式,因为我不知道如何将结果排序到新程序分类器的数据类型中 如果要将数据持久保存在文件中,则需要选择能够序列化数据的文件格式。python最常见的两种是
json
和pickle
import json
data = ['name', 'John Doe', 'scores', [1,2,3]]
# Serializes data and write it to file
with open('/path/to/file.txt', 'w') as f:
json.dump(data, f)
# Read the data back in
with open('/path/to/file.txt', 'r') as f:
read_data = json.load(f)
print(read_data)
# ['name', 'John Doe', 'scores', [1,2,3]]
您可能想阅读,任何需要阅读您的代码的人都会为此感谢您。正如现在写的,你的代码几乎是不可读的。对一个文件进行排序非常困难。你有没有想过使用sqlite?如果您不想这样做,最简单的方法是将整个文件读入内存,并用合适的键将其传递给排序function@e4c5你能帮我做这件事吗?嘿,这段代码会去哪里?这是什么类型的?