[Tutor] python problem

Alan G alan.gauld at freenet.co.uk
Thu May 26 23:05:52 CEST 2005


> i'm trying to write a code that handle errors help
>
> def print_menu(): ...

Personally I try to get the menu function to return the value selected
that way the meniu options and the code to check it is all wrapped
up in the same  place.
But that is only a side issue...

> while menu_choice != 5:
>     menu_choice = input("Type in a number (1-5):")
>     break
>         except(TypeError,NameError):

You need to use try: before an except.

So if you start your while loop body with a try

while menu_choice != 5:
   try:
      menu_choice = input(...)
      if menu_choice == 1:
        etc...
   except NameError, TypeError:
         # handle error here
        break  # only if you really need to..
   print_menu()

HTH,

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



More information about the Tutor mailing list