0
from random import randint
guessed = randint(0,2)
user_guess = input("What do you think the number I'm thinking of is?")

if user_guess == guessed:
    print("Correct!")
else:
    print("Incorrect!")

I'm looking for a way for the code to repeat or a way to print different messages until user_guess is the same as guessed.

If the person guesses incorrectly, I want to be able to tell them that, and then give them another chance.

Thanks, sorry for beginner question.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • 2
    Possible duplicate of [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – juanpa.arrivillaga Feb 06 '17 at 18:45

2 Answers2

0

Use a while loop.

from random import randint
correct = False
while (not correct): 
     guessed = randint(0,2)
     user_guess = int(input("What do you think the number I'm thinking of is?"))
     if user_guess == guessed: 
          print("Correct!")
          correct = True
     else: 
          print("Incorrect!")
Andrew Jenkins
  • 1,590
  • 13
  • 16
0

Stay in a while loop as long as the guess is wrong. You need the first guess to "prime" the loop.

# get the first user guess

while user_guess != guessed:
    print "Incorrect"
    # Get next user guess

Note that the code for "next user guess" will be very similar to "first user guess".

Prune
  • 76,765
  • 14
  • 60
  • 81