8.Your own while/else Cannot Win


#1



My code looks correct to me,but it does not! I can never get it to display the winning result.(It's probably because I did not indent properly but I am not sure how.)

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


It gives me the error
Oops, try again. Did you include an else for the while loop?


Replace this line with your code. 
from random import randint
guesses_left = 3
print (random_number)#Debug
random_number = randint(1, 10)
while guesses_left > 0:
    guess = int(raw_input("Your guess:"))
    if guess == random_number:
        print("You Win!")
        break
    else:
        print("You Lose")
        guesses_left -= 1


#2

You should decrement the guess before the else loop.

Take this up there.

to just before


#3

This is the code that I had created in my spare time to figure it out.

guessesLeft = 3
Begin the While loop
while guessesLeft > 0:
# Allow the user to see how many guesses they have left
print "Guesses remaining: ", guessesLeft
guess = int(raw_input("Guess a number: "))
# If/Else statement to determine win or loss.
if guess == randomNum:
print "Correct! You win!"
break
else:
print "Incorrect."
guessesLeft -= 1
# If user fails to guess the correct number, display "You lose."
else:
print "You lose."


#4

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("Your guess ?"))
guesses_left-=1

if guess==random_number:
    print "you win"
    break

else:
print "You lose"
guesses_left-1


#5

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