[Tutor] I'm puzzled

Shuying Wang shuying at gmail.com
Mon Jan 23 00:59:24 CET 2006


I'm guessing when you did that, you got something like an IndexError.
That's because you didn't check the length of "a" from raw_input and
accessed a nonexistent array element (a string is an array of
characters). So if you changed the line:
elif a[0] == '-' and a[1:].isdigit():
to :
elif len(a) > 1 and a[0] == '-' and a[1:].isdigit():

I expect you'll get what you want.

--Shuying

On 1/23/06, Vincent Zee <zenzee at xs4all.nl> wrote:
> Hi all,
>
> I'm puzzled (:-))
>
> 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():
>         print '- + isdigit'
>     elif a == 'q':
>         break
>     else:
>         print 'no digit'


More information about the Tutor mailing list