8/19 My code works but it isn't accpeted?


#1
from random import randint

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

guesses_left = 3
# Start your game!
while guesses_left > 0:
    guess = int(raw_input("Guess a number: "))
    print "You guessed: ", guess
    if guess == random_number:
        print 'You win!'
        break
    else:
        guesses_left = guesses_left - 1
        print 'You lose.'

My code works and runs fine, but it isn't accepted by Codecademy. Why?


#2

NVM, the code works, I just indented the else too far :smile:


#3

    guesses_left -= 1

may be written immediately following the guess input. That leaves,

else:
    print 'You lose.'

The else is part of the while statement, not the if statement. Back off the indent to match the while


#4

Hi,
I have the same problem, but when I move ELSE to matche the WHILE it gives me infinitive loop. What should I do?
thanks in advance


#5
    from random import randint

random_number = randint(1, 10)
guesses_left = 3

while guesses_left > 0:
    guess = int(raw_input("Your guess: "))

    if guess == random_number:
        print "You win!"
        break
    else:
        print "Wrong number!"
        guesses_left -= 1
else:
    print "You lose"

Try that, and guess what's different in this code.


#6

it worked. It makes sence to put 2 ELSE options. Great !
it's clear and logic,
thanks