[Tutor] I'm puzzled
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' /\ Vincent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I'm puzzled
Why will this little program crash when you enter the enter key? [program cut] Hi Vincent, What do you mean by crash? Do you get an error message? If so, can you show us? It will also help to think to try playing the situation out without preconceptions. In the beginning of the loop, at: a = raw_input('number? ') what does 'a' contain when you hit enter? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I'm puzzled
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 [EMAIL PROTECTED] 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' ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I'm puzzled
At 03:46 PM 1/22/2006, Vincent Zee wrote: Why will this little program crash when you enter the enter key? Thank you for including the traceback message in your 2nd post. Index error means you tried to reference an element of a sequence that is not there. a is the empty string when you just hit enter to the raw_input request. It therefore has no elements, so a[0] raises the exception. To avoid this test first for len(a) 0. 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' ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I'm puzzled
Hi Vincent, the program works with any input except when you just hit the enter key. To be able to understand why is the crash, take a look at what the interpreter tells you: File untitled.py, line 12, in ? elif a[0] == '-' and a[1:].isdigit(): IndexError: string index out of range IndexError is raised whan you try to access an element in a list or string, an element that does not exist. In the case where you only press enter, what is the content of a How many characters? (hint, you may try to print a before any evaluation... Hope that gets you going, Hugo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I'm puzzled
On Sunday, 22 January 2006 at 18:11:09 -0600, Hugo González Monteverde wrote: Hi Vincent, the program works with any input except when you just hit the enter key. To be able to understand why is the crash, take a look at what the interpreter tells you: File untitled.py, line 12, in ? elif a[0] == '-' and a[1:].isdigit(): IndexError: string index out of range IndexError is raised whan you try to access an element in a list or string, an element that does not exist. In the case where you only press enter, what is the content of a How many characters? (hint, you may try to Hi Hugo, thank you for your reply. What confused me was the fact that the isdigit method didn't complain about the empty string, so I assumed that indexing an empty string wouldn't be a problem (:-)) But now I think of it that wouldn't be logical. Sometimes the 'intelligence' of python makes me lazy (;-)) /\ Vincent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I'm puzzled
On Monday, 23 January 2006 at 10:59:24 +1100, Shuying Wang wrote: 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. Hi Shuying, thank you for your solution. /\ Vincent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor