<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>
<Below this line, add a link to the EXACT exercise that you are stuck at.>
<In what way does your code behave incorrectly? Include ALL error messages.>
From previous exercises, I thought that when doing an “if” loop, the “if,” “elif” and “else” commands were supposed to be inline with each other, like so:
number = 6
if number == 5:
elif number == 3:
But in this activity, the correct code has the “else” command lined up with the “while” command, and NOT lined up with the “if” command (see below).
<What do you expect to happen instead?>
I’m not sure; I just do not understand when things are considered to be “inside” of loops, when it matters, and indentation in general. I don’t feel like this ha been explained and I’m getting frequent error messages and unexpected behavior due to indentation problems, which is frustrating. Can someone help me out or refer me to a concept I may have missed?
This is the correct code from the exercise:
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!"
guesses_left -= 1
print “Guesses left: %s” %(guesses_left)
print “You lose.”
<do not remove the three backticks above>