我做了一個算術測驗,詢問你十個問題,你是在類中的.txt文件,用戶將放在那裏的名稱以及輸入1,2或3。然後它會詢問用戶十個問題,但最後它會將名稱和用戶分數的數據保存到名爲Class1Score,Class2Score或Class3Score的.txt文件中。如何訂購
這裏是我的代碼,顯示算術測驗:
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]]
這是保存它作爲我不知道如何將結果納入排序的格式新程序分揀機的數據類型。
您可能需要閱讀[PEP8蟒蛇風格指南](https://www.python.org/dev/peps/pep-0008/)任何必須閱讀你的代碼的人都會爲此感謝你。正如現在寫的,你的代碼幾乎是不可讀的。 –
排序文件是如此的1980年代你有沒有想過使用SQLite?如果你不想這樣做,,最簡單的將是整個文件讀入內存,並通過它用合適的按鍵功能 – e4c5
來排序@ e4c5能否請你幫我做這件事? – juniour