C. Ebert C. Ebert - 6 months ago 10
Python Question

How do I send the prompt backwards in my code in python?

I wrote a simple number guessing game with the

randomint
function.

Lets say I wrote it and it looked a little like this:

import blah
import blah

print ("Hello, and welcome to the guesser")

Answer1 = (randomint(1,5))
Guess1 = int(input("Input a number 1-5")

if Guess1 == Answer1:
print ("You got lucky that time")

elif Guess1 < 6 and != Answer1:
#send back to guess again

elif Guess1 > 5:
print ("Invalid Number")
#send back to guess again


It's probably a really stupid question and beginner stuff, but, is there a function that could fulfill my needs and wants of sending people back to guess the number again?

Answer

What you are searching for is called a loop in programming.

You could do something like this:

import random

Answer1 = random.randint(1,5)

while True:
    Guess1 = int(input("Input a number 1-5 >>"))

    if Guess1 == Answer1:
        print ("You got luck this time")
        break

    elif Guess1 < 6 and Guess1 != Answer1:
        #send back to guess again
        print ("Try again!")
        continue

    elif Guess1 > 5:
        print ("Invalid Number")
        #send back to guess again
        continue
Comments