Reputation:
Making a math quiz in python with random equations. I'm not sure how to stop the program from asking the same thing twice?
The program asks ten questions from a list of add, subtract or multiply, each using random numbers. I've managed to make the ten questions random, but I'm not sure how to stop it from choosing the same two numbers twice? For example, it would choose 1+3 for one question, but it will ask the same question multiple times after. 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
def add(a,b):
addQ = int(input(str(a) + "+" + str(b) + "="))
result = int(int(a) + int(b))
if addQ != result:
print ("Incorrect!", result)
else:
print("Correct")
a = random.randint(1,12)
b = random.randint(1,12)
def multiply(a,b):
multQ = int(input(str(c) + "X" + str(d) + "="))
results = int(int(c) * int(d))
if multQ != results:
print ("Incorrect! The answer is", results)
else:
print("Correct")
c = random.randint(1,12)
d = random.randint(1,12)
def subtract(a,b):
subQ = int(input(str(e) + "-" + str(f) + "="))
resultss = int(int(e) - int(f))
if subQ != resultss:
print ("Incorrect! The answer is", resultss)
else:
print("Correct")
e = random.randint(1,12)
f = random.randint(1,12)
while questions in range(10):
Qlist = [add, subtract, multiply]
random.choice(Qlist)(a,b)
questions += 1
if questions == 10:
print ("End of quiz")
Any help would be appreciated, thanks.
Upvotes: 0
Views: 774
Reputation: 55
for each problem you have asked, you can add it to a set of "asked_questions" and use "in" method to test if already asked, and if yes, generate a new question.
not sure if it's the most efficient method, but it definitely works, and more than enough for your little application.
Upvotes: 0
Reputation: 2150
The issue here is that you are generating random numbers at the start of the program, but not regenerating them every time a question is asked: your add
function will always use a
and b
, multiply
will always use c
and d
, and subtract
will always use e
and f
for all of their respective calculations, the values of which never change.
In addition, the parameters you are inputting are of no value for multiply
and subtract
as you are just disregarding them and using c, d
and e, f
respectively without note of the parameters inputted.
In order to remedy both these issues, I would place the random generation of the numbers inside the while
loop and make the functions use the correct inputted parameters for calculations.
Moreover, the while
iteration is a bit redundant a simple for questions in range(10)
without the extra bits is much more straightforward. Hence the questions
variable is useless.
With all that in mind, here is the rewritten 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!")
def add(a,b):
addQ = int(input(str(a) + "+" + str(b) + "="))
result = int(int(a) + int(b))
if addQ != result:
print ("Incorrect!", result)
else:
print("Correct")
def multiply(a,b):
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 i in range(10):
Qlist = [add, subtract, multiply]
random.choice(Qlist)(random.randint(1, 12),random.randint(1, 12))
print ("End of quiz")
The program could be refined further by creating a function that manages the printing and checking of the result dependent on a third parameter that dictates what operation it should perform.
Upvotes: 1