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 to restart this

I am making number guesser program and I am trying to figure out how to restart this if you get the number wrong. I have tried while true loops and It just keeps asking the question. I need some help with this thanks (python). EDIT: j1-lee answered question very good!

import random

ask = input("Guess a number between 0 and 10")

r1 = random.randint(0, 1)
print("The number is % s" %(r1))

if int(ask) == r1:
    print("right")
else:
    print("wrong")

>Solution :

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

Your while True approach was right. You only need to add break at an appropriate place:

import random

while True:
    ask = input("Guess a number between 0 and 10: ")
    r1 = random.randint(0, 10)
    print(f"The correct number is {r1}.")

    if int(ask) == r1:
        print("... and you were right!")
        break
    else:
        print("Try 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