+ 1
Python Maths Quiz - how to make sure random Num 1 is larger than Num 2 & number of loops = user input
import random def mathsQuiz(): noQns = int(input("Enter number of questions per level: ")) counter = 0 if counter <= noQns: level1num1 = random.randint(1,9) level1num2 = random.randint(1,9) if level1num1 > level1num2: counter = counter + 1 randomsym = random.choice(["+","-"]) print("Level 1") ans = input("{} {} {} = ".format(level1num1, randomsym,level1num2)) print(ans) counter = counter + 1 else: #level1num1 < level1num2: counter = counter - 1 mathsQuiz()
2 Respostas
0
To make the required number of loops, you say
for i in range(no_of_loops):
You can't make sure that one random number is greater than another random number as such, but you can pick Num 1 to be the higher of the two, and Num 2 to be the lower. Using the max() and min() methods, this can be done quite easily.
level1num1, level1num2 = max(level1num1, level1num2), min(level1num1, level1num2)
0
Great! It works perfectly. Thank you Russ !