如何保存分数,以便在程序再次运行时可以看到它们?

时间:2015-07-22 11:47:04

标签: python

import sys
import random
name=input("What is your name?")
print ("Alright",name,"welcome to your maths quiz")
score=0
level_of_difficulty = int(input(("What level of difficulty are you working at?\n"
                         "Press 1 for low, 2 for intermediate "
                         "or 3 for high\n")))

if level_of_difficulty != 1  or 2 or 3:
 sys.exit()

if level_of_difficulty == 3:
    ops = ['+', '-', '*', '/']
else:
    ops = ['+', '-', '*']

for question_num in range(1, 11):
    if level_of_difficulty == 1:
        number_1 = random.randrange(1, 10)
        number_2 = random.randrange(1, 10)
    else:
        number_1 = random.randrange(1, 20)
        number_2 = random.randrange(1, 20)

    operation = random.choice(ops)
    maths = round(eval(str(number_1) + operation + str(number_2)),5)
    print('\nQuestion number: {}'.format(question_num))
    print ("The question is",number_1,operation,number_2)

    answer = float(input("What is your answer: "))
    if answer == maths:
    print("Correct")
    score = score + 1
    else:
         print ("Incorrect. The actual answer is",maths)

if score >5:
    print("Well done you scored",score,"out of 10")
else:
    print("Unfortunately you only scored",score,"out of 10. Better luck next     time")

这是我正在使用的代码,但现在我想添加一个名为类号的新变量,并将学生分数保存到他们的班级。如果一个新学生在哪里参加测验他们的分数也应该保存,他们应该能够查看以前学生的分数。我尝试通过写这个来做到这一点,但我不知道如何使用搁置功能或我是否应该使用它。我知道有类似的问题已经回答,但我不理解他们请帮助我,这是我尝试开始,但我不知道如何继续。

            import sys
            import random
            import shelve
            score=0
            name=input("What is your name?")
            print ("Alright",name,"welcome to your maths quiz")
            class_number = int(input("Before you start, are you in class 1, 2 or 3?"))# This line doesn't work
            if class_number != 1  or 2 or 3:
                sys.exit()
            if class_number == 1:
                open(class_1_scores, w)
                level_of_difficulty = int(input(("What level of difficulty are you working at?\n"
                                         "Press 1 for low, 2 for intermediate "
                                         "or 3 for high\n")))

            if level_of_difficulty != 1  or 2 or 3:
                sys.exit()

            if level_of_difficulty == 3:
                ops = ['+', '-', '*', '/']
            else:
                ops = ['+', '-', '*']

            for question_num in range(1, 11):
                if level_of_difficulty == 1:
                    number_1 = random.randrange(1, 10)
                    number_2 = random.randrange(1, 10)
                else:
                    number_1 = random.randrange(1, 20)
                    number_2 = random.randrange(1, 20)

                operation = random.choice(ops)
                maths = round(eval(str(number_1) + operation + str(number_2)),5)
                print('\nQuestion number: {}'.format(question_num))
                print ("The question is",number_1,operation,number_2)

                answer = float(input("What is your answer: "))
                if answer == maths:
                    print("Correct")
                    score = score + 1
                else:
                    print ("Incorrect. The actual answer is",maths)
            if score >5:
                print("Well done you scored",score,"out of 10")
            else:
                print("Unfortunately you only scored",score,"out of 10. Better luck next time")


            if class_number == 2:
                open(class_2_scores, w)
                level_of_difficulty = int(input(("What level of difficulty are you working at?\n"
                                         "Press 1 for low, 2 for intermediate "
                                         "or 3 for high\n")))

            if level_of_difficulty != 1  or 2 or 3:
                sys.exit()

            if level_of_difficulty == 3:
                ops = ['+', '-', '*', '/']
            else:
                ops = ['+', '-', '*']

            for question_num in range(1, 11):
                if level_of_difficulty == 1:
                    number_1 = random.randrange(1, 10)
                    number_2 = random.randrange(1, 10)
                else:
                    number_1 = random.randrange(1, 20)
                    number_2 = random.randrange(1, 20)

                operation = random.choice(ops)
                maths = round(eval(str(number_1) + operation + str(number_2)),5)
                print('\nQuestion number: {}'.format(question_num))
                print ("The question is",number_1,operation,number_2)

                answer = float(input("What is your answer: "))
                if answer == maths:
                    print("Correct")
                    score = score + 1
                else:
                    print ("Incorrect. The actual answer is",maths)
            if score >5:
                print("Well done you scored",score,"out of 10")
            else:
                print("Unfortunately you only scored",score,"out of 10. Better luck next time")

            if class_number == 3:
                open(class_3_scores, w)
                level_of_difficulty = int(input(("What level of difficulty are you working at?\n"
                                         "Press 1 for low, 2 for intermediate "
                                         "or 3 for high\n")))

            if level_of_difficulty != 1  or 2 or 3:
                sys.exit()

            if level_of_difficulty == 3:
                ops = ['+', '-', '*', '/']
            else:
                ops = ['+', '-', '*']

            for question_num in range(1, 11):
                if level_of_difficulty == 1:
                    number_1 = random.randrange(1, 10)
                    number_2 = random.randrange(1, 10)
                else:
                    number_1 = random.randrange(1, 20)
                    number_2 = random.randrange(1, 20)

                operation = random.choice(ops)
                maths = round(eval(str(number_1) + operation + str(number_2)),5)
                print('\nQuestion number: {}'.format(question_num))
                print ("The question is",number_1,operation,number_2)

                answer = float(input("What is your answer: "))
                if answer == maths:
                    print("Correct")
                    score = score + 1
                else:
                    print ("Incorrect. The actual answer is",maths)
            if score >5:
                print("Well done you scored",score,"out of 10")
            else:
                print("Unfortunately you only scored",score,"out of 10. Better luck next time")

3 个答案:

答案 0 :(得分:0)

您可以尝试字典。

class_x_scores = {}
class_x_scores[name] = score 

最后,您可以将这些dicts保存到文件中:

f = open(class_x_scores,'a') # a for adding newlines to the file without cleaning information
for name in class_x_scores:
    f.write(" The score of "+name + " is "+class_x_scores[name])# any format you prefer
f.close()

答案 1 :(得分:0)

您应该使用pandas

from pandas import DataFrame as df

d = df({'Student name': ['John', 'Joe', 'Lilly'], 'Score': [5.6, 2.7, 3.4]})

d.save('filename')

d.load('filename')

print d

答案 2 :(得分:-1)

  

我如何保存分数?

这只是意味着你想拥有一个非常简单的数据存储......可能是

  1. XML文件
  2. JSON文件
  3. 或简单的txt文件
  4. 任何您喜欢的文件
  5. 是什么让你停止使用上述任何文件。?