[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
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
Re: [Tutor] How to get terminal settings
On Thursday, 19 January 2006 at 16:03:16 -0800, Terry Carroll wrote: On Thu, 19 Jan 2006, Vincent Zee wrote: Any pointers to other curses howto's? There's http://heather.cs.ucdavis.edu/~matloff/Python/PyCurses.pdf ; but it's mostly a couple of example programs, without a lot of explanation. Hi Terry, thank you for the link, I'll have a look. /\ Vincent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get terminal settings
On Thursday, 19 January 2006 at 23:53:06 -, Alan Gauld wrote: Assuming you are on a Unix style OS/terminal you can read the output of stty. Look at the rows value Here are a couple of runs of rows at different terminal sizes: $ stty -a | grep rows speed 38400 baud; rows 41; columns 80; line = 0; $ stty -a | grep rows speed 38400 baud; rows 26; columns 80; line = 0; A call to Popen should get you what you want. An alternative technique, and the one useed by the real more/less programmes is to use curses to open a display window. Hi Alan, this has helped me very much, thank you. I'll use the popen() before venturing into curses land. /\ Vincent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to get terminal settings
Hi, say you want to write a more-like program. How do you know how many lines the terminal window can display. /\ Vincent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor