[Tutor] What's wrong with this code?

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Jul 7 10:31:43 CEST 2005

Hi Nathan,

Other folks are pointing out why you're getting certain error messages.
You may also want to consider breaking up the long second-half of the
program into into its own set of functions.  I believe that the second
half could look something like this:

while menu_choice != 9:
    menu_choice = input("Choose an option:")
    if menu_choice == 1:
    elif menu_choice == 2:
    elif menu_choice == 3:
    elif menu_choice == 4:
    elif menu_choice == 5:
    elif menu_choice == 6:

The idea behind the break-up here is to make it a little easier to see, at
a high level, how the program works.  As a side benefit, it should help to
avoid wacky things like certain menu choices munging up the variables used
by other menu choices.

Finally, by physcially shortening that loop, it's easier to see that we'll
always ask for a menu_choice whenever we hit the start of the loop.  In
contrast, the original code is so long that we may have forgotten, which
is why each menu command appears to repeat the "Choose an option:"

(When you learn a little more about Python, we can talk about the concept
of "dispatch tables" to further dissolve that code, but let's talk about
that later.)

I guess I'm trying to say: style can mattes because it can help you fix
your program's bugs faster.  *grin*

Best of wishes to you!

More information about the Tutor mailing list