将最后三个分数保存到文件中

时间:2016-01-19 21:36:00

标签: python file-writing

我有一项任务是创建一个测试学生数学技能的程序。我完成了任务1和2,然后进行测验,然后将其保存到文件中,但现在任务3,我现在必须保存学生的最后3分,这是我真正在努力的。

这是我的代码:

print("Good job",name,"! You have finished the quiz") #congrats user for finnsihing quiz
print("You got" + str(score) + " /10 questions correct!") #gives user final score out of 10
class_name = "class 1.txt"    # creates a file under the name a user specifies
with open(class_name, "r") as f:
    lines = f.readlines()
    lines.append("{name} : {score}\n".format(name=name, score=score))
    with open(class_name, "w") as f:  # re-opening as "w" will blank the file
        for line in sorted(lines):
            f.write(line)

0 个答案:

没有答案