[Tutor] NEWBIE QUEASTION ALERT - how to restart program?

Magnus Lycka magnus@thinkware.se
Fri Dec 20 20:33:02 2002


Hi Ole,

At 01:10 2002-12-21 +0100, Ole Jensen wrote:
>Alright, as the subject says - Im not the most educated man in python (or 
>any other language), but I been going through some tutorials.

We all have to crawl before we can walk. No worry.

>If you run this program independently the window will close automaticly 
>after it has been run, now instead of just closing the window how would it 
>be possiple to re-run the program, e.g. in the first many you have a 
>choice of: 1 or 2. if the user presses any other key, the program will 
>print "grow up man... 1 or 2!!!" and shut down/close the window (given 
>after waiting 5 seconds), now instead I would like the program to ask the 
>question again... how is that done...?
>
>given it maight not be crutial, but I really would like to know how its 
>done. and finally I should say I have no understanding of how the loops 
>work (I've gone through tutorials about the FOR-loops and WHILE-loops but 
>they make no sense to me) so if loops are esantial to what i want to 
>achive I was wondering if any of you reading this could guide me to some 
>very basic walk-through's of loops

In this case, loops are exactly what you want. I hope I can
walk you through this.

A while-loop, which is what you would use here, is very similar to
an if statement. You understand the if-statement, right? Run the
intented code after the if-statement if the expression evaluates
to a value we consider as true. (I.e. not 0, or something empty.)

A while loop runs the indented block repeatedly as long as the
expression is true. Like this:

# menu
shape = 0
while shape is not in [1, 2]:
     print "Select a shape"
     print "1: Quadrangle"
     print "2: Circle"
     shape = input(">>>")

if shape...

Let's walk through this:

shape = 0 # This is just to make sure that the next line does what we want.
while shape is not in [1, 2]:  # shape is 0, neither 1 or 2, so the 
condition is true, run the indeted block
     print bla bla bla...
     shape = input(">>>") # Let's imagine I enter '5'
# The next line is not indented, so we have reached the en of the while-block
# So far, this is exactly as an if-statement, right? But hang on, now it 
gets different.
# After the end of the block, we return to the while-line again!
while shape is not in [1, 2]:  # shape is 5, so the condition is true, run 
the indeted block
     print bla bla bla...
     shape = input(">>>") # Let's imagine I enter '1'
while shape is not in [1, 2]:  # shape is 1, so the condition is true, end 
the loop
if shape... # whatever...

Obviously, it's importent that something happens in the while block that might
change the while-condition, or we will be stuck in the loop for ever. (Not 
completely
true, but let's leave break for later.)
>print ""

To print an empty line, just type

print

An empty string doesn't make any difference.
>    else:
>         print "SHUT THE XXXX UP XXXXX!!!"

Really, I don't think this kind of language is appropriate here,
and if you send things like that to a mailing list, you are likely
to get auto-responses from mail-filtering software here and there.
That happened to me when I included a somewhat colourful Monty Python
quote in an email to this very establishment...
>         sleep(5)

Instead of sleep, I'd use

raw_input('Press ENTER to continue')

raw_input() is like input, but it doesn't evaluate what you type,
it always return a string. Normally you'd use "x = raw_input(..."
but in this particular case, we don't care what it returns, we just
want it to prevent the program from rushing ahead...
>
>elif shape == 2:
>     print "circle"
>     R = input("What is the radius?")
>     print ""
>     print "The area of the circle is:",R**2*3.14

import math
R**2*math.pi

is more exact... (If you want an approximation, use 355./113.)

To return to your original question:

To be able to repeat the whole process, not just the little piece
I did above, you put the whole block inside a big while loop.

But if a loop spans too many lines, let's say more lines than fits
in your screen, it get's more difficult to follow what's happening.
In those cases, it's probably a good idea to split up the program
into several separate functions, instead of one big sequence like
you did. But let's take one step at a time.


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se