[Tutor] HANDLING ERRORS

Feziwe Mpondo feziwe at sanbi.ac.za
Thu Jun 30 14:32:17 CEST 2005


HI
help mi. here's what i tried but it's not working
print "Type Control C or -1 to exit"

def print_menu():
    print '1. Print Phone Numbers'
    print '2. Add a Phone Number'
    print '3. Remove a Phone Number'
    print '4. Lookup a Phone Number'
    print '5. Quit'
    print
numbers = {}
menu_choice = 0
print_menu()
while menu_choice != 5:

    try:

        menu_choice = input("Type in a number (1-5):")
        if menu_choice == 1:
                print "Telephone Numbers:"
                for x in numbers.keys():
                    print "Name: ",x," \tNumber: ",numbers[x]
                print

        except ValueError:
             print "Oops! That was no valid number.  Try again..."

       elif menu_choice == 2:
           print "Add Name and Number"
           name = raw_input("Name:")
           phone = raw_input("Number:")
           numbers[name] = phone
       elif menu_choice == 3:
           print "Remove Name and Number"
           name = raw_input("Name:")
           if numbers.has_key(name):
               del numbers[name]
           else:
                print name," was not found"
       elif menu_choice == 4:
           print "Lookup Number"
           name = raw_input("Name:")
           if numbers.has_key(name):
               print "The number is",numbers[name]
           else:
                print name," was not found"
       elif menu_choice != 5:
           print_menu()


More information about the Tutor mailing list