Menu Interface Problem.

Paulo Repreza pxrepreza at gmail.com
Tue Mar 10 01:48:07 EDT 2009


Hi all,

I'm having a little problem in my program Menu Interface.

Here is the code:

-----------------------------------------------------------------------------------------------------------------
# Improvement of lists-demolists.py **Includes a MENU interface*

menu_item = 0
list = []

# Program exits until 'menu_item = 9'
while menu_item != 9:
    print '-----------------'
    print '1. Print the list.'
    print '2. Add a name to the list.'
    print '3. Remove a name from the list.'
    print '4. Change an item in the list.'
    print '9. Quit'
    menu_item = input('Pick an item from the menu: ')

    # TO-DO for option '1'.
    if menu_item == 1:
        current = 0
        if len(list) > 0:
            while current < len(list):
                print current,'. ',list[current]
                current = current + 1
        else:
            print 'List is empty.'

    # TO-DO for option '2'.
    elif menu_item == 2:
        name = raw_input('Type in a name to add: ')
        list.append(name)

    # TO-DO for option '3'.
    elif menu_item == 3:
        del_name = raw_input('What name would you like to remove: ')
        if del_name in list:
            item_number =  list.index(del_name)
            del list[item_number]
            # The code above only removes the first occurance of
            # the name. The code below from G removes all.
            # while del_name in list:
            # item_number = list.inder(del_name)
            # del list[item_number]
        else:
            print del_name, 'was not found.'

    # TO-DO for option '4'.
    elif menu_item == 4:
        old_name = raw_input('What name would you like to change: ')
        if old_name in list:
            item_number = list.index(old_name)
            new_name = raw_input('What is the new name: ')
            list[item_number] = new_name
        else:
            print old_name, ' was not found.'

print "Goodbye"

-----------------------------------------------------------------------------------------------------------------------------------------------------

Well the program runs, but my issue it's when I try to enter a letter
instead of a number (I do this to check if I will see the Menu regarding if
I enter a wrong character) but when I enter a character(string) I get this:

--------------------------------------------------------------------------------------------------------
Pick an item from the menu: f
Traceback (most recent call last):
  File "lists-improve-demolists.py", line 14, in <module>
    menu_item = input('Pick an item from the menu: ')
  File "<string>", line 1, in <module>
NameError: name 'f' is not defined
-------------------------------------------------------------------------------------------------------

I've noticed that the 'menu_option; var' it's not a string so that's why I
see this error, but I tryed by doing str(menu_option) and it works, but when
I enter a digit 1, 2, 3, 4, 9 it won't take me to that particular option,
instead it shows the menu all over again and the only way I can exit the
program is by doing ctrl + C.

Any hint on how I can change the code in order for me to use the menu
regarding if is a string or an integer (menu_option)?

Thank You!

Paulo Repreza
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090309/aea65a7f/attachment.html>


More information about the Python-list mailing list