[Tutor] Convert String to Int

Sridhar Ratnakumar sridhar.ratna at gmail.com
Mon Jan 19 02:32:33 CET 2009


On Sun, Jan 18, 2009 at 5:15 PM, Ian Egland <echolynx at gmail.com> wrote:
> I know that, should you want to get an int from the user, you use
> int(input("Question!")). However, what if the user wasn't that savvy and
> didn't realize he/she HAD to enter a number? Program crash. Is there a way
> that I can get the input as a string, check and see if it's convertible to
> an integer, convert it if it is and ask them to try again if it's not?

while True:
    try:
        a = int(raw_input())
        break
    except ValueError:
        print "Try again"

print 'You entered:', a


More information about the Tutor mailing list