Your own while / else (improving it)


#1



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


The code works fine, but I was wondering if there was a way to make sure the raw_input deals with inputs that are letters instead of numbers


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
    else:
        guesses_left-=1
        print "Try Again"
else:
    print "You Lose!"


#2

well, you can get the input, then check if the input is blank (empty string) or .isalpha() before converting to integer

If you want to cover more cases, you will need code we haven't learned yet (try/except)


#3

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