[Tutor] Interactive Menu Woes

Wesley Brooks wesbrooks at gmail.com
Wed Nov 14 12:49:58 CET 2007


In the middle of your addName function you have defined another
function that requires 'self' and you've got a default argument
'get=1';

def numberType(self, get = 1):

You call this further down the function with the line;

self.type = numberType(self.get)

This should have been;

self.type = numberType(self)

I would suggest moving the numberType function from inside the addName
function as follows;

class MenuInput:
    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 : "))
        type = self.numberType(get)
        enter = phoneentry()
        enter(name, number, returnType)

    def numberType(self, get):
        if get == 1:
            returnType = HOME
        elif get == 2:
            returnType = WORK
        elif get == 3:
            returnType = FAX
        return returnType

If you use self in a function all functions can see the value without
having to have it in the function definition.

For example;

class a:
    def a(self):
        self.get = 1
    def b(self):
        print self.get

>>> aa = a()
>>> aa.a()
>>> aa.b()
1
>>>

Default arguments in the function definition work as follows:

class a:
    def a(self):
        self.get = 1
    def b(self, get="Using default argument"):
        print get

>>> aa = a()
>>> aa.a()
>>> aa.b()
"Using default argument"
>>> aa.b(1)
1
>>>

Cheers,

Wes.

On 14/11/2007, Bryan Magalski <bryan_magalski at yahoo.com> wrote:
>
> Greetings all!!
>
> This is my first post and I am going to probably be vague at first, but, I
> will try my best to be specific.  I am a very GREEN scripter/programmer, so
> please be as descriptive as possible.  Ok, enough excuses, on with my
> question!
>
> Here is my issue:
>
> 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)
>
>         def display(self):
>                 print "Enter a name to look up: (leave blank to exit)"
>                 self.Name = str(raw_input("Name: "))
>                 print "%s has the following information: " % self.Name
>                 if self.Name != "":
>                         foo = phonedb()
>                         for entry in foo.lookup(self.Name):
>                                 print '%-40s %s (%s)' % (entry.name,
> entry.number, entry.showtype())
>                                 print
> [/code]
>
> when I instantiate and run it with:
>
> [code]
> menu = MenuInput()
> menu.addName()
> [/code]
>
> and enter the information asked, I am given this runtime error:
>
> [error]
> To add a name, please input he following information:
> Name: Bryan
> Number: 1234567
> What type of number is this? (choose one):
>  1. Home:
>  2. Work:
>  3. Cell:
>  : 1
> Traceback (most recent call last):
>   File "examples\testpy\my_object_modified.py", line 101,
> in <module>
>     menu.addName()
>   File "examples\testpy\my_object_modified.py", line 85, in
> addName
>     self.type = numberType(self.get)
>   File "examples\testpy\my_object_modified.py", line 74, in
> numberType
>     if self.get == 1:
> AttributeError: 'int' object has no attribute 'get'
> >>>
> [/error]
>
> I "think" that this has something to do with passing the results or possibly
> my functions are not properly calling (proper terminology?) each other.
>
> Now what/where am I wrong?  As this is part of an object oriented
> programming class, I wanted the menu constructed inherent to the class.
>
> Thank you in advance.  --Bryan
>  ________________________________
> Be a better sports nut! Let your teams follow you with Yahoo Mobile. Try it
> now.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>


More information about the Tutor mailing list