Home > Enterprise >  My python code won't give me other random numbers
My python code won't give me other random numbers

Time:10-28

My code keeps printing out the same numbers even thou i answer the question correctly. This is supposed to be a math game were the code randomizes operations of 2 numbers that the user has to guess

import random

print("Enter A for addition:")
print("Enter B for subtraction:")
print("Enter C for multiplication:")
print("Enter D for division:")
print("Enter E for exit:")
options=str(input("Hello,Please enter an option:"))
                    
num1 = random.randrange(1, 21)
num2 = random.randrange(1, 21)

if options== 'E':
    print("Enter E(In caplocks to quit):")
    exit()


while options== 'A':
    real1=num1 num2
    print("What is",num1," ",num2,":")
    answer_question=int(input("Hello,Please enter an the answer to the question:"))
    if answer_question == (real1):
        print("You are correct!")
    else:
        print("You are incorrect!!!!")

CodePudding user response:

Each time your while loop runs, you need to create two new random numbers. Since you originally had those numbers outside of your loop, they would never change they were outside of the scope of your while loop.

import random

print("Enter A for addition:")
print("Enter B for subtraction:")
print("Enter C for multiplication:")
print("Enter D for division:")
print("Enter E for exit:")
options = str(input("Hello,Please enter an option:"))

#num1 = random.randrange(1, 21) #Move these into your while loop
#num2 = random.randrange(1, 21) #Move these into your while loop

if options == 'E':
    print("Enter E(In caplocks to quit):")
    exit()

while options == 'A':
    num1 = random.randrange(1, 21) # <--
    num2 = random.randrange(1, 21) # <--
    real1 = num1   num2
    print("What is", num1, " ", num2, ":")
    answer_question = int(input("Hello,Please enter an the answer to the question:"))
    if answer_question == (real1):
        print("You are correct!")
    else:
        print("You are incorrect!!!!")

CodePudding user response:

import random

print("Enter A for addition:")
print("Enter B for subtraction:")
print("Enter C for multiplication:")
print("Enter D for division:")
print("Enter E for exit:")
options=str(input("Hello,Please enter an option:"))
                    

if options== 'E':
    print("Enter E(In caplocks to quit):")
    exit()


while options== 'A':
    num1 = random.randrange(1, 21)
    num2 = random.randrange(1, 21)
    real1=num1 num2
    print("What is",num1," ",num2,":")
    answer_question=int(input("Hello,Please enter an the answer to the question:"))
    if answer_question == (real1):
        print("You are correct!")
    else:
        print("You are incorrect!!!!")
  • Related