Your own while/ else


#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("Your guess: "))

if guess == random_number:
print "You win!"
break
guesses_left = -1
else:
print “You lose.”

ERROR-
File “python”, line 14
guesses_left = -1
^
IndentationError: unexpected indent

LINK-
https://www.codecademy.com/en/courses/learn-python/lessons/loops/exercises/your-own-while--else#


#2

What are you doing here
A break will EXIT the loop

guesses_left = -1
 else:
 print “You lose.”

Please re-edit your post…
select the code you are presenting
and click the :Preformatted text </> button


#3

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
break
else:
print “You lose.”

I changed where I put the break,

ERROR-
File “python”, line 14
SyntaxError: ‘break’ outside loop


#4

We will then be able to see which indentation you are using…
and we will be able to help

Read discusions / opinions about how to use a break statement
https://stackoverflow.com/questions/6346492/how-to-stop-a-for-loop
Look at the code they are using as examples, especially the indentations they use !!!


#5

ERROR-
File “python”, line 14
guesses_left = -1
^
IndentationError: unexpected indentation

Syntax error:
guesses_left -= 1


#6

Try by putting a 4-spaces in front of
guesses_left = -1
so in contrast to your IF statement start position you would have something
like

if condition:
    #4 extra spaces for the IF-code-block
    guesses_left = -1
else:
    #4 extra spaces for the ELSE-code-block

#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.