My own while/else 8.19 please help me!


#1

I have allready try all types but i can"t done it.my code is look like this but i have message like this Oops, try again. Did you include an else for the while loop?
from random import randint

random_number = randint(1, 10)
print random_number
guesses_left = 3
while guesses_left>0:
guess=int(raw_input("enter your number"))
if guesses_left==guess:
print "You win!"
guesses_left -=1
break
else:
print "You lose"
break


#2

Hi, @marina8811 ,

Whether the else block matches the while loop or the if block depends upon how the code is indented. For this exercise, it needs to match the while loop.

We cannot see the indentation because your posted code is not formatted. Please format code when you post it. See How do I format code in my posts?.


#3

from random import randint

random_number = randint(1, 10)
print random_number
guesses_left = 3
while guesses_left>0 :
-----guess=int(raw_input("enter your number"))
-----if guesses_left==guess:
-----print "You win!"
-----guesses_left -=1
-----break
else:
-----print "You lose"

   Did you allow the user 3 guesses, or did you incorrectly detect a correct guess? now I have this kind of error   how I think  guesses_left -=1  this line doesn''t work correct


#4

Is this the appropriate test for a correct guess? ...

if guesses_left==guess:

Is this indented enough to be within the if block? ...

print "You win!"

#5

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