# Your own while / else

#1

Hi everyone,
the error says: Did you include an else for the while loop?
What's wrong? I couldn't find the right solution.

``````from random import randint

# Generates a number from 1 through 10 inclusive
random_number = randint(1, 10)

guesses_left = 3
while guesses_left>0:
guesses_left = guesses_left - 1
if guess==random_number:
print "You win!"
break
else:
print "You lose."``````

#2

This problem asks you to use While/else statement.
refer to -- >

Your code will be fine if you indent your else to same indentation as while's!
I hope this hint works for ya!

#3

thanks a lot! it worked

#4

Youre welcome!!

#5

Bearing in mind that I am a novice, how can I make my grammar bettter in line 18 where it gives me "You have 1 GUESSES left"? I know it is not important and not required in this exercise.

from random import randint

# Generates a number from 1 through 10 inclusive

random_number = randint(1, 10)

guesses_left = 3

while guesses_left >0:

``````guess=int(raw_input(" guess a number between 1 and 10 inclusive "))

if guess ==random_number:# note here that the computer chooses the number

print ("You win.")
break

guesses_left-=1
print ("You have ")+str(guesses_left) + (" guesses left")``````

else:
print ("You lose")

guess a number between 1 and 10 inclusive 4
You have 2 guesses left
guess a number between 1 and 10 inclusive 3
You have 1 guesses left
guess a number between 1 and 10 inclusive 2
You have 0 guesses left
You lose
None

#6

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