function - 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 list of add, subtract or multiply, each using random numbers. i've managed make ten questions random, i'm not sure how stop choosing same 2 numbers twice? example, choose 1+3 1 question, ask same question multiple times after. here code:

import random #asks name name = input("what's name?") #stops user entering invalid input when entering class classchoices = ["a","b","c"] classname = input("what class in?") while classname not in classchoices:     classname = input("not 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")  = 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! 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! 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 appreciated, thanks.

the issue here generating random numbers @ start of program, not regenerating them every time question asked: add function use a , b, multiply use c , d, , subtract use e , f of respective calculations, values of never change.

in addition, parameters inputting of no value multiply , subtract disregarding them , using c, d , e, f respectively without note of parameters inputted.

in order remedy both these issues, place random generation of numbers inside while loop , make functions use correct inputted parameters calculations.

moreover, while iteration bit redundant simple for questions in range(10) without bits more straightforward. hence questions variable useless.

with in mind, here rewritten code.

import random #asks name name = input("what's name?") #stops user entering invalid input when entering class classchoices = ["a","b","c"] classname = input("what class in?") while classname not in classchoices:     classname = input("not 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! 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! answer is", resultss)     else:         print("correct")   in range(10):     qlist = [add, subtract, multiply]     random.choice(qlist)(random.randint(1, 12),random.randint(1, 12))  print ("end of quiz") 

the program refined further creating function manages printing , checking of result dependent on third parameter dictates operation should perform.


Comments