Math Game
# 15.06.2020
#Math Game
#Stephanie
#I only added a question for input from the user at the point were the total questions need to be defined
#otherwise it's the same code as in the resources
def calculateAnswer(lhs, rhs, operator):
if (operator == "-"):
return lhs - rhs
if (operator == "*"):
return lhs * rhs
if (operator == "/"):
return lhs / rhs
if (operator == "+"):
return lhs + rhs
from random import randint
def generateQuestion():
ops = "/*-+"
opIndex = randint(0, len(ops) - 1)
operator = ops[opIndex]
lhs = randint(0, 10)
rhs = randint(0, 10)
while(rhs == 0 and operator =="/"):
rhs = randint(0, 10)
return lhs, rhs, operator
#for i in range(100):
#results = generateQuestion()
#if(results[1] == 0):
#print(generateQuestion())
def isAccurateEnoughAnswer(givenAnswer, correctAnswer, tolerance = 0.01):
difference = abs(float(givenAnswer) - float(correctAnswer))
return difference <=tolerance
totalQuestions = input("How many Questions would you like to answer? ")
totalQuestions = int(totalQuestions)
correct = 0
for i in range(totalQuestions):
question = generateQuestion()
correctAnswer = calculateAnswer(question[0], question[1], question[2])
playerAnswer = input("{0} {2} {1} = ".format(question[0], question[1], question[2]))
if(str(correctAnswer)) == playerAnswer:
print("Correct!")
correct += 1
else:
print("Wrong. Correct answer = " + str(correctAnswer))
print("Yout got {0} correct out of {1}. Or {2}% correct.".format(correct, totalQuestions, correct/totalQuestions*100))