Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I make my code not generate new integers when the user gets the answer wrong?

What the code below does is that it generates 2 random integers and multiplies them together. After the user has placed an input and gets it corrects, it will automatically generate another pair of random integers.

How could I make it so that when the user input is wrong, it does not generate a number but waits till the user to finally calculate the correct answer and then it generates a new set of integers?

I’ve been playing around with having a loop within the main loop but it hasn’t solved my problem

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

while loop:

new_ints = True

while new_ints:

    random_int_1 = random.randint(2,9)
    random_int_2 = random.randint(2,9)

    answer = random_int_1*random_int_2

    equation = (f"{random_int_1}x{random_int_2} = ? ")
    print(equation)

    user_answer = int(input())

    if answer == user_answer:
        print("correct")
        new_ints = True
        loop = True
    else:
        print("Wrong")
        new_ints = True
        loop = False

>Solution :

Instead of the if statements you can simply have another while loop to check the guess like this:

while int(input()) != answer:
    print('Incorrect. Guess again')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading