Hi 

How would I do this?
The teacher wants to keep track of the scores each member of the class obtains 
in the quiz. There are three classes in the school and the data should be kept 
separately for each class.

Here is my code:

import random
import operator

MATHS_OPERATIONS = [
    (operator.add, "+"),
    (operator.mul, "*"),
    (operator.sub, "-")
    ]

NUM_QUESTIONS = 10

def get_int_input(prompt=''):
    while True:
      try:
        return int(input(prompt))
      except ValueError:
        print("Not a valid input (integer is expected)")

def get_bool_input(prompt=''):
    while True:
        val = input(prompt).lower()
        if val == 'yes':
            return True
        elif val == 'no':
            return False
        else:
            print("Not a valid input (yes/no is expected)")

if __name__ == '__main__':
    name = input("What is your name?").title()
    class_name = input("What is your Class? ")
    print(name, ", Welcome to the Maths Test")

    score = 0
    for _ in range(NUM_QUESTIONS):
        num1 = random.randint(1,100)
        num2 = random.randint(1,100)
        op, symbol = random.choice(MATHS_OPERATIONS)
        print("What is", num1, symbol, num2)
        if get_int_input() == op(num1, num2):
            print("Correct")
            score += 1
        else:
            print("Incorrect")

    print("Well done", name, "you scored", score, "/", NB_QUESTIONS)

    filename = class_name + ".txt"

    with open(filename, 'a') as f:
        f.write(str(name) + " : " + str(score) + '\n')

    if get_bool_input("Do you wish to view previous results for your class"):
        with open(filename, 'r') as f:
            print(f.read())
    else:
        input ("Press any key to exit")

Could somebody here me please?
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to