-2
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")

That is the code i am working with but now I want to add a new variable called class number and save the students scores to their class. If a new student where to take the quiz their score should save to it as well and they should be able to view the scores of previous students. I tried do do this by writing this but I don't know how to use the shelve function or whether i should use it at all. I know there are similar questions already answered but I didn't understand them please help me and here is my attempt to start but i don't know how to carry on.

            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 Answers3

0

You can try Dictionaries.

class_x_scores = {}
class_x_scores[name] = score 

In the end, you can save these dicts to files:

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()
Wang Bucca
  • 98
  • 5
  • Thanks a lot Wang but if I would like to add what level of difficulty the student was working at would I have to define it at the dictionaries at the start of the program – John knight Jul 22 '15 at 13:05
  • yes,you can use a list to store both the difficulty and score to the name,just like this: class_x_scores[name]=[score,difficulty]; – Wang Bucca Jul 22 '15 at 14:01
0

You should use 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
Geeocode
  • 5,705
  • 3
  • 20
  • 34
-1

How would I save the scores?

This simply mean you want to have a very simple data storage..which may be either

  1. XML file
  2. JSON file
  3. Or a simple txt file
  4. Any kind of your own preferable file

What make you stop from using any above files.?

pmverma
  • 1,653
  • 13
  • 27