building a small calculator

Brian van den Broek bvande at po-box.mcgill.ca
Wed Apr 20 13:08:38 EDT 2005


aleksander.helgaker at gmail.com said unto the world upon 2005-04-20 08:41:
> Sorry. This is my first day using Python.
> 
> Is there an equivelent to the goto command? I want to find some way to
> take the user back to the main menu.
> 


When I first started learning Python some long time after having done 
some BASIC, I found the lack of goto a puzzle, too. You'll get over it :-)

Looking at the code from your original post, you just have a linearly 
ordered list of statements, with very little structure. It is much 
easier to control program flow in Python if you employ functions.

Keeping in mind that this is a sketch, and I am no expert on Python, 
but a hobbyist, try changing it so it is more like this pseudo-code

def print_intro():
     # intro display logic here

def prompt_user():
     # menu display and input fetching logic here

def rectangle_stuff():
     # etc.

def circle_stuff():
     #

etc.

then,

def main():
     while True:
         prompt_user()
         # logic to do the right thing with the input goes here
         # there are better ways, but as a first go, you could use
         # if user_input_value == '1':
         #     rectangle_stuff()
         # etc
         if user_input_value == quit_value:
             break # or call an exit function to do other stuff on exit

Fill out those functions, and call them in the appropriate order, and 
the main function will continue to prompt the user, do what's wanted, 
and repeat until the user enters the menu option for quiting. (Once 
again, a rough first approximation.)

I'd second the recommendation you received to check out the Tutor list 
-- both lists are friendly, but Tutor is perhaps better geared to the 
level of explanation that you might find most helpful in these early 
stages.

Best,

Brian vdB





More information about the Python-list mailing list