Python数学测验从txt文件导入数据并组织i

2024-10-03 17:22:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在做OCR计算机科学A453 CA,总结一下我被赋予了3项任务,我已经完成了前2项,但最后一项任务确实给我带来了问题。我的出发点是把数学考试的分数存到txt中。文件。子项的名称和分数将保存在与其类相关的文件中。对于任务3,我现在被要求导入这些数据,以找到平均分数和最高分数,并按从高到低的顺序排列,以及按字母顺序排列的分数。我试了又试,但找不到解决这个问题的办法。我已经删除了我所有的实验代码,试图让它发挥作用,并只留下你的代码,直到分数被保存。这将是一个伟大的帮助,找到一个解决方案,甚至是一些好的指针,对什么做!谢谢:)

代码:

username=input("What is your name?") #childs name variable
group=int(input("What class number are you, 1, 2 or 3?")) #this determines which file the score will be saved in
print ("Welcome {} to the Arithmetic quiz".format(username))#The introduction to the quiz

correctAnswers=0
for question_number in range(10):
    if test():
        correctAnswers +=1

print("{}: You got {} answers correct".format(username, correctAnswers))


if group==1:#If the childs class 1,their score will be saved in the file for the class:1
    with open("class1.txt","a+") as f:
        f.write("{}:{}".format(username,correctAnswers))
if group==2:#If the childs class is equal to 2, the file will be saved in the file for class:2
    with open("class2.txt","a+") as f:
        f.write("{}:{}".format(username,correctAnswers))
if group==3:#If the childs class is equal to 3, the file will be saved in the file for class:3
    with open("class3.txt","a+") as f:
        f.write("{}:{}".format(username,correctAnswers))         

Tags: thetointxtformatgroupusernamebe