How can I get this code to repeat and num_1 & num_2 to generate a new random number until user types Q?
import random
num_1 = random.randrange(1, 15)
num_2 = random.randrange(1, 15)
score = 0
added = num_1 num_2
while True:
print("Press Q to quit anytime")
answer = input("What is " str(num_1) " " str(num_2) " = ")
if answer == "q":
print("Bye")
break
if added == int(answer):
print("Great, try another one")
score = 1
else:
print("Try a new one")
CodePudding user response:
You're declaring num_1 and num_2 with random. This sets them when you make the declaration so you're always adding the same two numbers. You're not assigning these to a function but a static return.
CodePudding user response:
You have to change the values of num_1
and num_2
after each iteration. To do so you can add the following two lines
num_1 = random.randrange(1, 15)
num_2 = random.randrange(1, 15)
at the end of the while
loop.
You can also define a function that returns two random numbers and use it instead, something like this
def get_two_random_numbers(lower=1, upper=15):
return random.randrange(lower, higher), random.randrange(lower, higher)
num_1, num_2 = get_two_random_numbers()