8. Your own while / else - trying to make it a better game


#1



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


Here is the error:
File "python", line 15
else:
^
SyntaxError: invalid syntax

What do you expect to happen instead?
I expect the else statement to work perfectly fine. I don't understand why there is a syntax error. This is NOT required in the instructions but I wanted to incorporate what I've learned from other lessons. Any ideas? Thanks!


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:
    print "Please guess a number between 1 and 10: "
    guess = int(raw_input("Your guess: "))
    if guess == random_number:
        print "Dang, you are good! That's correct!"
        break
    guesses_left -= 1
    else:
        print "Nope, try again! You are within %s of the number!" % random_number - guess
else:
    print "You lose."


#2

Then do that on your own, not in the lesson. Embellishment or deviation from the instructions is never a good idea. It messes with the lesson checker, and usually introduces errors that are a disruption to the normal Q&A process intended.

For best results, stay on station with the exact exercise instructions. Once you pass the module, you can go to town, but better if you discuss your ideas and code in the Corner Bar, away from people who are just looking for code to copy so they can complete their homework faster.


#3

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