[Tutor] Interactive Menu Woes
Bryan Magalski
bryan_magalski at yahoo.com
Wed Nov 14 21:40:04 CET 2007
Thank you for your suggestion. I did not create the original script, so it will stay as is and my addition for the menu has been adjusted.
Now that I can make a clear distinction of what I am returning, I am getting a new error that leads me that I am trying to call a function that cannot be seen when instantiated:
To add a name, please input he following information:
Name: Bryan
Number: 1234567890
What type of number is this? (choose one):
1. Home:
2. Work:
3. Cell:
: 1
Traceback (most recent call last):
File "menu_modified.py", line 95, in <module>
menu.addName()
File "menu_modified.py", line 73, in addName
enter(name, number, returnType)
AttributeError: phoneentry instance has no __call__ method
[code]
class MenuInput:
# ask user to input data and store to be passed to manageable variables.
def addName(self):
print "To add a name, please input he following information: "
name = raw_input("Name: ")
number = raw_input("Number: ")
get = int(raw_input("What type of number is this? (choose one): \n \
1. Home:\n \
2. Work:\n \
3. Cell:\n \
: "))
returnType = self.numberType(get)
#print returnType
enter = phoneentry()
enter(name, number, returnType)
[/code]
I googled the __call__ method, but I am not getting to the meat of what all of the leads have to say. I know it has to deal with when I instantiated phoneentry() within addName(), I just don't know how to fix it or "call" it properly.
Again, thanks in advance.
----- Original Message ----
From: Evert Rol <evert.rol at gmail.com>
To: Bryan Magalski <bryan_magalski at yahoo.com>
Cc: tutor at python.org
Sent: Wednesday, November 14, 2007 1:28:45 PM
Subject: Re: [Tutor] Interactive Menu Woes
> I am trying to build a menu for the following script to make it
> more "user friendly". Nothing fancy, just a simple add data and
> look up the entered results.
>
> The problem is that when I run my modified version with this
> snippet (please see attachment for original and my modified
versions):
>
> [code]
> class MenuInput:
>
> # ask user to input data and store to be passed to manageable
> variables.
> def addName(self):
> print "To add a name, please input he following
> information: "
> self.enter = phoneentry()
> self.name = raw_input("Name: ")
> self.number = raw_input("Number: ")
> self.get = int(raw_input("What type of number is
> this? (choose one): \n 1. Home:\n 2. Work:\n 3. Cell:\n : "))
> def
numberType(self, get = 1):
> if self.get == 1:
> self.returnType = HOME
> #return self.getType
> elif self.gete == 2:
> self.returnType = WORK
> #return self.getType
> elif self.get == 3:
>
self.returnType = FAX
> #return self.getType
> return self.returnType
>
> self.type = numberType(self.get)
> self.enter(self.name, self.number, self.returnType)
>
Bypassing the whole 'function within function' problem, consider
using a dictionary instead: somedict = {1: HOME, 2: WORK, 3: FAX},
and self.type = somedict[self.get]. This also avoids endless if-elif-
elif-elif sequences (looking at showtype() in your original script,
where you can
apply the same technique); it's often clearer, and
certainly shorter.
____________________________________________________________________________________
Never miss a thing. Make Yahoo your home page.
http://www.yahoo.com/r/hs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071114/7b793947/attachment-0001.htm
-------------- next part --------------
A non-text attachment was scrubbed...
Name: menu_modified.py
Type: text/x-python
Size: 2832 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20071114/7b793947/attachment-0001.py
More information about the Tutor
mailing list