8. Your own while / else


#1

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

Instructions:
1. Use a while loop to let the user keep guessing so long as guesses_left is greater than zero.
2. Ask the user for their guess, just like the second example above.
3. If they guess correctly, print 'You win!' and break.
4. Decrement guesses_left by one.
5. Use an else: case after your while loop to print You lose..

Error Message:
Oops, try again. Your code looks a bit off. Check the Hint if you need help! Your code threw the following error: invalid literal for int() with base 10: ''

Error Message on Console:
Traceback (most recent call last):
File "python", line 9, in
ValueError: invalid literal for int() with base 10: ''

I don't really get what this is asking me to do....

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

#2

Have you tried entering a number at the input prompt (in the console after submitting)?

That line is after a break, so is unreachable. Where should that code be placed?


#3

check the indentation of both:

guesses_left -= 1

else:


#4

2 posts were split to a new topic: 8. your own while else


#5

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