[Tutor] Linear programming - is it bad?

Alan Gauld alan.gauld at blueyonder.co.uk
Tue Aug 10 09:24:34 CEST 2004


> messages from mailing list. If you'd like to see the source code,
heres
> a link to it. (Hope my old pentium who hosts the file will survive
the
> slashdot effect ;)
>
> http://home.bilange.ca/files/index.php?dir=&file=AutoLogC.py

I just noticed that you ask some questions in the code so I
thought I'd answer a few of them:

> #Is there a pythonized version of DOS cls?
> system("cls")

No, clearing the screen is a very OS depemdant thing,
for example on Unix its "clear" instead of CLS. There
is a module that Fred Lundh(sp?) wrote that tries to
provide OS independant behaviour but I don't think
it ever made it into the standard library.

> #looks like raw_input is the only choice for dealing with user
input.
> #unfortunately, the user has to press enter, since its not a
> one-key press event.
> blah = raw_input("> ")

You can use getch() as well which just pulls the current
keyboard state, that avoids the need for hitting enter.
But because its instantaneous you need to put it in a loop:

key = getch()
while not key:
  key = getch()

Or for your purpose, simply detecting a keystroke:

while not getch(): pass

Finally you have 4 try/except blocks looking for the same exception.
You could do it in one by setting a status flag after each stage,
it just tidies the code a little. But as Danny has shown you could use
functions here much more effectively.

Linear style programming is OK for *very short* programs but yours has
some repeating code so functions are better for that as well as
making it more readable.

Hope the above pointers help,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld/tutor2/



More information about the Tutor mailing list