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

Don Arnold Don Arnold" <darnold02@sprynet.com
Fri Dec 20 20:18:01 2002

----- Original Message -----
From: Ole Jensen
To: tutor@python.org
Sent: Friday, December 20, 2002 6:10 PM
Subject: [Tutor] NEWBIE QUEASTION ALERT - how to restart program?

Alright, as the subject says - Im not the most educated man in python (or
any other language), but I been going through some tutorials.
And as I have expanded the trials in order not to just copy and paste I was
wondering how it is possible to go back in the script/file in order to
restart certain section in order for the program not just to shut down...
well e.g.:

This program has been modified from the area calculator found in the instant
hacking tutorial http://www.hetland.org/python/instant-hacking.php
<source snipped>

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

--- my reply: ---
Loops _are_ essential to what you are trying to do: repeatedly execute a
body of suite of code. Python has 2 flavors of loop. The 'for' loop is
designed to repeat a set number of times. It executes your loop body once
for each item in the given sequence:

>>> for number in (1,2,3,4,5):
 print number, number * 5

1 5
2 10
3 15
4 20
5 25

The 'while' loop will execute its body as long as its condition is true.
It's an indefinite loop because it doesn't run a specific number of times.
Instead, some action inside the loop body eventually invalidates the loop
condition, causing it to terminate.

>>> from random import randint
>>> number = 0
>>> while number != 10:
 number = randint(1,10)
 print number


This is exactly the behavior you want: you want to repeat the loop until the
user chooses to quit. You can do this by adding a 'quit' option to your main
menu and then moving the code that should repeat inside a 'while' loop:

# Import the sleep mode in order to display results before shut down
from time import sleep

#flag to see if we're done
done = 0

# Area calculator
print "------------------------------"

while not done:
    # menu
    print ""
    print ""
    print "Select a shape"
    print "1: Quadrangle"
    print "2: Circle"
    print "3: Quit"

    shape = input(">>>")

    if shape == 1:
        print "Is it a:"
        print ""
        print "1: square"
        print "2: rectangle"
        quad_shape = input(">>>")

        if quad_shape == 1:
            print "Square"
            S = input("What is the lenght of any side?")
            print ""
            print "The area is",S**2

        elif quad_shape== 2:
            print "rectangle"
            L = input("What is the lenght?")
            W = input("What is the width?")
            print ""
            if L == W:
                print "this not a rectangle moron!"
                print "The area is",L*W

            print "wrong key!!!"

    elif shape == 2:
        print "circle"
        R = input("What is the radius?")
        print ""
        print "The area of the circle is:",R**2*3.14

    elif shape == 3:
        done = 1

        print "Grow up man... 1, 2 or 3!!!"

## we fell out of the loop
print 'Finished.'