Loops 8/19; You Own While/Else


#1

Can anyone help me figure out why I'm getting this error message. It prints just fine to the console but it doesn't seem to like my else statement. Thanks!


While loop error
#2

The else should be under the while, not the if. Back of the indentation.


#3

Thank you for your prompt reply. I moved the else under the while and still getting the same error message. I played with the indentation for a bit thinking the if should be under while as well but no luck. I'm stumped.


#4

Please provide:

  1. A link to the exercise
  2. Your code

Then we can take a better look. Thank you.


#5

https://www.codecademy.com/en/courses/python-beginner-en-cxMGf/0/8?curriculum_id=4f89dab3d788890003000096

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

It passed this way but I went back a few minutes later and gives error msg again with same indentation.


#6

I made this change, and the code ran to completion with no errors:

    if guess == random_number:
        print "You win!"
        break
    elif guesses_left > 0:
        print 'Guess again.'

SCT message

Did you print the "You lose." message?

Your code has a misspelled word, 'loose' => 'lose'


#7

Thank you much for you help. I realized my code only passed when I guessed correctly. I corrected my spelling error, runs with no issues now.

Thanks again.


#9

A post was split to a new topic: 8/19 Does not work for me