8.Your own while/else Cannot Win


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.)


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!")
        print("You Lose")
        guesses_left -= 1


You should decrement the guess before the else loop.

Take this up there.

to just before


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!"
print "Incorrect."
guessesLeft -= 1
# If user fails to guess the correct number, display "You lose."
print "You lose."


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 ?"))

if guess==random_number:
    print "you win"

print "You lose"


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