如何订购.txt文件

时间:2016-05-07 10:26:17

标签: python sorting format

我做了一个算术测验,询问你十个问题和你所在的课程。用户会在那里输入名称,然后输入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]]

这是它保存的格式,因为我不知道如何将结果排序到新程序分类器的数据类型中。

1 个答案:

答案 0 :(得分:0)

如果要将数据保留在文件中,则需要选择能够序列化数据的文件格式。 python的两个最常见的是jsonpickle

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]]