0

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?

C. Ebert
  • 15
  • 3
  • just use a while loop – polku May 24 '16 at 08:56
  • 1
    Wait...you're kidding me all of that work and I didn't even have to write something other than a while loop? I knew I hated loops from the beginning. – C. Ebert May 24 '16 at 08:58
  • Well, I think you are quite new to programming, but this what imperative programming is about: control flow (loops, conditions, ...) – exilit May 24 '16 at 09:00
  • Unfortunately a major problem in Python is the absence of goto, so you can't really avoid loops :) – polku May 24 '16 at 09:07

1 Answers1

1

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
exilit
  • 1,156
  • 11
  • 23