0

Below I have created a Maths quiz in python. It consists of random equations to solve. The questions are generated using random numbers and functions that determine whether the question is add or subtract. I have completed the quiz, but I have tried adding a score counter so that if the user gets a question right, a point is added on. I am unsure how to do this, as I have tried implementing the score into the functions and it does not work. Here is the code:

import random
#Asks for name
name = input("What's your name?")
#Stops user from entering invalid input when entering their class
classchoices = ["A","B","C"]
classname = input("What class are you in?")
while classname not in classchoices:
    classname = input("Not a valid class, try again:")

print(name, ",", classname)
print("Begin quiz!")

questions = 0
a = random.randint(1,12)
b = random.randint(1,12)


def add(a,b):
    addQ =  int(input(str(a) + "+" + str(b) + "="))
    result = int(int(a) + int(b))
    if addQ != result:
        print ("Incorrect! The answer is", result)

    else:
        print("Correct")





def multiply(a,b):
    score = 0
    multQ =  int(input(str(a) + "X" + str(b) + "="))
    results = int(int(a) * int(b))
    if multQ != results:
        print ("Incorrect! The answer is", results)

    else:
        print("Correct")



def subtract(a,b):
    subQ =  int(input(str(a) + "-" + str(b) + "="))
    resultss = int(int(a) - int(b))
    if subQ != resultss:
        print ("Incorrect! The answer is", resultss)
    else:
        print("Correct")

for questions in range(10):
    Qlist = [add, subtract, multiply]
    random.choice(Qlist)(random.randint(1,12), random.randint(1,12))
    questions += 1



if questions == 10:
    print ("End of quiz")
Cœur
  • 37,241
  • 25
  • 195
  • 267

4 Answers4

1

You can return a score for each task. An example for the add function:

if addQ != result:
    print ("Incorrect! The answer is", result)
    return 0

else:
    print("Correct")
    return 1

Of course you can give higher scores for more difficult tasks (e.g. 2 points for multiplications).

and then below:

score = 0
for questions in range(10):
    Qlist = [add, subtract, multiply]
    score += random.choice(Qlist)(random.randint(1,12),random.randint(1,12))
    questions += 1
mcb
  • 398
  • 2
  • 12
0

Use a global var and add +1 on every correct solution. The only negative is as soon as the programm is closed the score is lost. something like this

Community
  • 1
  • 1
F. Aker
  • 27
  • 1
  • 9
0

Using return values of your functions as demonstrated by cello would be my first intuition.

But since you are already using a global variable to track your questions, you can introduce another one for the score.

questions = 0
score = 0
a = random.randint(1,12)
b = random.randint(1,12)

To use it (and not a local variable score, which would be only available in the function), your functions have to reference it by using globals:

def add(a,b):
    global score

You can then increment it in the else-statements of your functions:

else:
    print("Correct")
    score += 1

At the end, you can print the score:

print("End of quiz")
print("Your score:", score)

Notice, that you are using questions variable in a strange fashion:

You first initialize it with 0:

questions = 0

Then you overwrite it during a loop:

for questions in range(10):
    questions += 1

The for will give questions the values 0..9, so it is totally unneccessary to increase the number manually inside the loop. This increased value will immediately be overwritten by the next value of the loop.

After your loop exits, you are sure to have asked 10 questions. The following if-condition is superfluous (and only works, because you additionally increase questions in the loop). Just lose it:

print("End of quiz")
Christian König
  • 3,437
  • 16
  • 28
0

You missed to define score globally and then access it within the function and updating it, Here's a working version:

import random
#Asks for name
name = input("What's your name?")
#Stops user from entering invalid input when entering their class
classchoices = ["A","B","C"]
classname = input("What class are you in?")
while classname not in classchoices:
    classname = input("Not a valid class, try again:")

print(name, ",", classname)
print("Begin quiz!")

score = 0
questions = 0
a = random.randint(1,12)
b = random.randint(1,12)


def add(a,b):
    global score
    addQ =  int(input(str(a) + "+" + str(b) + "="))
    result = int(int(a) + int(b))
    if addQ != result:
        print ("Incorrect! The answer is", result)

    else:
        score += 1
        print("Correct")

def multiply(a,b):
    global score
    multQ =  int(input(str(a) + "X" + str(b) + "="))
    results = int(int(a) * int(b))
    if multQ != results:
        print ("Incorrect! The answer is", results)

    else:
        score += 1
        print("Correct")

def subtract(a,b):
    global score
    subQ =  int(input(str(a) + "-" + str(b) + "="))
    resultss = int(int(a) - int(b))
    if subQ != resultss:
        print ("Incorrect! The answer is", resultss)
    else:
        score += 1
        print("Correct")

for questions in range(10):
    Qlist = [add, subtract, multiply]
    random.choice(Qlist)(random.randint(1,12), random.randint(1,12))
    questions += 1

if questions == 10:
    print ("End of quiz")
    print('Score:', score)
JkShaw
  • 1,927
  • 2
  • 13
  • 14