Hi all,<br><br>I'm having a little problem in my program Menu Interface.<br><br>Here is the code:<br><br>-----------------------------------------------------------------------------------------------------------------<br>
# Improvement of lists-demolists.py **Includes a MENU interface*<br><br>menu_item = 0<br>list = []<br><br># Program exits until 'menu_item = 9'<br>while menu_item != 9:<br> print '-----------------'<br>
print '1. Print the list.'<br> print '2. Add a name to the list.'<br> print '3. Remove a name from the list.'<br> print '4. Change an item in the list.'<br> print '9. Quit'<br>
menu_item = input('Pick an item from the menu: ')<br> <br> # TO-DO for option '1'.<br> if menu_item == 1:<br> current = 0<br> if len(list) > 0:<br> while current < len(list):<br>
print current,'. ',list[current]<br> current = current + 1<br> else:<br> print 'List is empty.'<br> <br> # TO-DO for option '2'.<br> elif menu_item == 2:<br>
name = raw_input('Type in a name to add: ')<br> list.append(name)<br> <br> # TO-DO for option '3'.<br> elif menu_item == 3:<br> del_name = raw_input('What name would you like to remove: ')<br>
if del_name in list:<br> item_number = list.index(del_name)<br> del list[item_number]<br> # The code above only removes the first occurance of<br> # the name. The code below from G removes all.<br>
# while del_name in list:<br> # item_number = list.inder(del_name)<br> # del list[item_number]<br> else:<br> print del_name, 'was not found.'<br> <br> # TO-DO for option '4'.<br>
elif menu_item == 4:<br> old_name = raw_input('What name would you like to change: ')<br> if old_name in list:<br> item_number = list.index(old_name)<br> new_name = raw_input('What is the new name: ')<br>
list[item_number] = new_name<br> else:<br> print old_name, ' was not found.'<br><br>print "Goodbye"<br><br>-----------------------------------------------------------------------------------------------------------------------------------------------------<br>
<br>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:<br>
<br>--------------------------------------------------------------------------------------------------------<br>Pick an item from the menu: f<br>Traceback (most recent call last):<br> File "lists-improve-demolists.py", line 14, in <module><br>
menu_item = input('Pick an item from the menu: ')<br> File "<string>", line 1, in <module><br>NameError: name 'f' is not defined<br>-------------------------------------------------------------------------------------------------------<br>
<br>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.<br>
<br>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)?<br><br>Thank You!<br><br>Paulo Repreza<br>