I am new to python coding.
I am writing a program, a quiz that asks mathematical questions, it will ask 3 maths question and
randomly choose one of 4 operators, +,-,*,/
if the quizzer gets the question right the score will go up by one
I have tested this code many times but it has never done * or divide, it only does + and -
also sometimes the scoring works fine and other times the scoring does not work at all
I am writing a program, a quiz that asks mathematical questions, it will ask 3 maths question and
randomly choose one of 4 operators, +,-,*,/
if the quizzer gets the question right the score will go up by one
I have tested this code many times but it has never done * or divide, it only does + and -
also sometimes the scoring works fine and other times the scoring does not work at all
Python:
import random
#Defining Addition, Subtraction, Multiplication, Division
def add(x1, x2):
return x1 + x2
def sub(x1, x2):
return x1 - x2
def mul(x1, x2):
return x1 * x2
def div(x1, x2):
return x1 / x2
#this function will return the users answer for the mathematical quiz question
def check(x1,x2):
answer = int(input("What is the value"))
return answer
def quiz():
#this will keep a count of how many times the question was asked,
#the quiz will end when 3 questions have been asked
count = 0;
#this will keep a score of how many questions the player got right,
score =0;
while (count !=3):
value = random.randint(0,3)
if value == 0:
x1 = int(input('First number?\n'))
x2 = int(input('Second number?\n'))
print("What is the answer of", x1, "+", x2, "= ")
if add(x1,x2) == check(x1,x2):
score +=1
count +=1
elif value == 1:
x1 = int(input('First number?\n'))
x2 = int(input('Second number?\n'))
print("What is the answer of", x1, "-", x2, "= ")
if sub(x1, x2) == check(x1,x2):
score +=1
count +=1
elif value == 2:
x1 = int(input('First number?\n'))
x2 = int(input('Second number?\n'))
print("What is the answer of", x1, "-", x2, "= ")
if mul(x1, x2) == check(x1,x2):
score +=1
count +=1
elif value == 3:
x1 = int(input('First number?\n'))
x2 = int(input('Second number?\n'))
print("What is the answer of", x1, "-", x2, "= ")
if div(x1, x2) == check(x1,x2):
score +=1
count +=1
if count == 3:
print("You have scored ", score, "out of 3")
exit()
quiz()