[Tutor] I'm puzzled

gmane emile at fenx.com
Tue Jan 31 02:07:09 CET 2006


> Why will this little program crash when you enter the
> enter key?
>
> while True:
>    a = raw_input('number? ')
>    if a.isdigit():
>        print 'isdigit'
>    elif a[0] == '-' and a[1:].isdigit():

You could instead use

    elif a.startswith('-') and a[1:].isdigit():

as the empty string certainly doesn't start with '-', and python won't check 
the second half after the first check evaluates false.


>        print '- + isdigit'
>    elif a == 'q':
>        break
>    else:
>        print 'no digit'





More information about the Tutor mailing list