Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Menu Interface Problem.

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


pxrepreza at gmail

Mar 9, 2009, 10:48 PM

Post #1 of 2 (221 views)
Permalink
Menu Interface Problem.

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


gagsl-py2 at yahoo

Mar 10, 2009, 6:11 AM

Post #2 of 2 (205 views)
Permalink
Re: Menu Interface Problem. [In reply to]

En Tue, 10 Mar 2009 03:48:07 -0200, Paulo Repreza <pxrepreza [at] gmail>
escribió:

> # 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:

input() is rather confusing (and is gone in Python 3). It does two things:
- retrieve some text typed by the user
- evaluate it as if it were an expresion
That means that typing 1+2 works (and will choose option 3), but typing f
will raise an exception (there is no "f" variable). I suggest you forget
about input and use raw_input instead:

menu_item = raw_input('Pick an item from the menu: ')

This returns a string (like '1'). So change all your "if" statements
accordingly:

# TO-DO for option '1'.
if menu_item == '1':

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.