-2

I'm new to Python so I'm not really sure on the basics so I understand this is probably a stupid question.

I'm making code where the user inputs a number, and if they get it wrong, I want the code to restart and ask them to guess again till it's right. How do I do this?

print("Let's play a game! Type in a number and see if you guess mine correctly!")
num = input("Type in a number: ")
for x in range (0,1):
    if num == "7":
        print("Correct!")
    else:
        print("Nope, try again!")

(Also I know that a lot of this code is probably wrong.)

khelwood
  • 55,782
  • 14
  • 81
  • 108
Milly.S
  • 1
  • 1
  • 5
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Peter Wood Oct 31 '18 at 13:20

1 Answers1

0

You can put it in a while loop:

verified = None
while verified is None:
    num = input("Type in a number: ")
    if num == "7":
        print("Correct!")
        # if answer is good
        verified = True
    else:
        print("Nope, try again!")
Nouman
  • 6,947
  • 7
  • 32
  • 60
Ethan Koch
  • 267
  • 1
  • 6