# 8/19 Your own while/ else

#1

So, I run this and the game seems to run perfectly:

``````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 = guesses_left - 1
else:
print "You lose."``````

why is it that it gives me the following error:

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

#2

Might want to take a closer look at the instructions, you've got an if-else, the exercise is about while-else

#3

the instructions are the following:
Instructions
1.Use a while loop to let the user keep guessing so long as guesses_left is greater than zero.

1. Ask the user for their guess, just like the second example above.

2. If they guess correctly, print 'You win!' and break.

3. Decrement guesses_left by one.

4. Use an else: case after your while loop to print You lose..

on the third step it asks for an if, and that is what I put into my code. Later on it also asks for an 'else:', which I also included into my code. Can you please clarify on what it is i did wrong or could do better?

#4

Do you know the difference between if-else and while-else?

The rest of the instructions explain that.

#5

Yes, after looking back at the instructions I understand it. However, doing as instructed, the console would not print the "You lose.". Is there any way to follow a while-else and still have it print "You lose."?

Code update:

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

#6

Did you win? If so it shouldn't print that you lost. Try losing.

Also, you can't do anything after breaking.

#7

I tried playing various times and i didn't always win. It still wouldn't say "You lose.". Sorry for taking long to reply, I was preoccupied.

Here is the console's output:

You win!
None

#8

Well if it's not saying that you lose, then what does it say?

``````\$ python2 dezmen.py

It just.. keeps asking. It shouldn't do that. It has to stop asking before it can do things after the loop, such as telling you that you lost.

#9

I'm pretty sure I found your problem, although you may have solved it by now. Indentation is key in programming. The line that reduces guesses_left is nested under the win condition and the break that stops the loop, so guesses_left is never decreased. If you were to simply bring the guesses_left decrease code to happen after the if rather than in the if it should work!

``````if .....
print...
break
guesses_left -= 1``````

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

I know Codecademy is not the best when it comes to instructions so don't feel bad. It took me a half hour to get this part down.

#11

while guesses_left > 0:
guess = int(raw_input(“Guess a # 1-9:”))
if guess == random_number:
print “You win!”
break
else:
guesses_left = guesses_left - 1
else:
print “You lose.”
This is correct

#12
``````while guesses_left > 0:
if guess == random_number:
print 'You win!'
break
guesses_left -= 1
else:
print 'You lose'
``````

it says there’s something wrong with the if statement… can someone help?

#13

This works fine. The guesses_left should be decremented outside IF statement and inside WHILE loop

from random import randint
random_number = randint(1, 10)# Generates a number from 1 through 10 inclusive
guesses_left = 3
while guesses_left > 0:
guesses_left = guesses_left - 1
if guess == random_number:
print “You win!”
break

``````         # Start your game!
``````

else:
print “You lose”

#15

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

change to

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

#16

your ‘guesses_left = guesses_left - 1’ shouldn’t be part of your if-statement.
Indent it to same line as the if-statement.
Your else and if have similar indentation. While and else ought to be having a similar indentation and not if

#17

You had done a infinite loop, because you never decrement the guesses_left if you realize. This is because you will decrement when you hit the right number but when you doit you will end the game. So guesses_left must be on the else statement.

#18

from random import randint

# Generates a number from 1 through 10 inclusive

random_number = randint(1, 10)

guesses_left = 3