I have tried and tried. I have used ones from this forum and they fail. This was my last attempt. Anyone out there with something that works
all the time?

``````from random import randint

# Generates a number from 1 through 10 inclusive
random_number = randint(1, 10)

guesses_left = 3
while guesses_left > 0:
if guess == random_number:
print "You win!"
break

elif guesses_left -= 1:
print 'Guess again.'
else:
print "You lose."
``````

Slight change-up:

``````while guesses_left > 0:
guesses_left -= 1
if guess == random_number:
print "You win!"
break
elif guesses_left > 0:
print 'Guess again.'
else:
print "You lose."
``````

I at one time had this. I get this;

``````Oops, try again.
Did you include an else for the while loop?
``````

My typing mistake. Back the `else:` off to match the `while`. I’ll edit my post above to correct it.

Wow. Thank you. So just getting rid of the indent on else solved it? I thought if, elif, and else were to be indented. wow.

There are two control flow structures in play… while and if. They both have an else clause (optional). In this case, the elif is part of the if statement, and the else is part of the while statement.

1 Like

from random import randint

# Generates a number from 1 through 10 inclusive

random_number = randint(1, 10)

guesses_left = 3

while guesses_left>0:
if guess==random_number:
print “You win!”
break
guesses_left -=1
else:
“You lose”

but it continues to tell me that i have a syntax error
File “python”, line 10
if guess==random_number:
^
SyntaxError: invalid syntax

Guys you are making it more complicated than it needs to be. Just think about it logically.

Here is my code:

from random import randint

# Generates a number from 1 through 10 inclusive

random_number = randint(1, 10)

guesses_left = 3