8/19 - what is wrong with this code?


#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 < 4:
guess = int(raw_input("Guess a number: "))
if guess == random_number:
print 'You win!'
break
guesses_left = guesses_left - 1
else:
print 'You lose.'


#2

That's ok with your code, seems that it is codecademy mistake. Just delete identations before else (like it is not an if else block), and it will work.


#3

when i do that it says " SyntaxError: invalid syntax"


#4

You probably need to import the random module

from random import randint

Just tested you code and it worked I only added the import statement


#5

I agree with you that when I delete the idention before 'else', it works


#6

It works for me with the else condition

from random import randint
guesses_left = 0

Generates a number from 1 through 10 inclusive

random_number = randint(1, 10)
while guesses_left == 3:
guess = int(raw_input("Your guess: "))
print guess
if guess == random_number:
print " you win!"
break
guesses_left -= 1
else:
print " you lose and start over"


#7

Please review this code. It works.

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("Your guess: "))
    if guess == random_number:
        print 'You win!'
        break
    guesses_left -= 1
else:
    print 'You lose.'