Your own while / else (improving it)


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!"
        print "Try Again"
    print "You Lose!"


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)


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