[Tutor] Interactive Menu Woes

Bryan Magalski bryan_magalski at yahoo.com
Thu Nov 15 13:25:12 CET 2007


I wish I would have read your response before posting to Evert Rol's response.  They compliment each other in both explanation and detail.  I thank you as well for your contribution, as I said in my response to Evert, I will post my script completed when I get home.

Thank you.


--Bryan


----- Original Message ----
From: Wesley Brooks <wesbrooks at gmail.com>
To: Bryan Magalski <bryan_magalski at yahoo.com>
Sent: Thursday, November 15, 2007 10:12:26 AM
Subject: Re: [Tutor] Interactive Menu Woes

No worries, I'm still learning myself - be it four years down the
line. Don't think you ever really finsh learning these languages,
there's always new tricks to learn!

The __init__ functions are called when you create an instance of a
class, and are optional.

class a:
    def __init__(self, text):
        self.text = text

    def PrintText(self):
        print self.text

>>> aa = a("Testing __init__")
>>> aa.PrintText()
Testing __init__
>>>

More useful for the telephone number example would be an init function
that created a dictionary to contain your name/number information;

class Directory:
    def __init__(self):
        self.directory = {}

    def AddNumber(self, name, number):
        # name should be a string,
        # number can be string or integer.
        self.directory[name] = number

    def RetrieveNumber(self, name):
        nameList = self.directory.keys() # Returns a list of all the
names in the dictionary
        if name not in nameList: # Checks to make sure the name is in the list
                                            # if this check was not
made an error would be raised
                                            # if you tried to get the
name from the dictionary.
            print "Sorry, do not have that persons name!"
        else:
            return self.directory[name]

>>> telObj = Directory()
>>> telObj.AddNumber("Damion", 666)
>>> telObj.RetrieveNumber("Eric")
Sorry, do not have that persons name!
>>> telObj.RetrieveNumber("Damion")
666

However you would like to give these people different types of numbers
etc so you could create a new dictionary to store various details
inplace of the numbers:

class Directory:
    def __init__(self):
        self.directory = {}

    def AddDetail(self, name, type, data):
        if name not in self.directory.keys():
            self.directory[name] = {}
        personDetails = self.directory[name]
        personDetails[type] = data

    def RetrieveDetail(self, name, type):
        nameList = self.directory.keys()
        if name not in nameList:
            print "Sorry, do not have that persons name!"
        else:
            personDetails = self.directory[name]
            storedDetailTypes = personDetails.keys()
            if type not in storedDetailTypes:
                print "Sorry, do not have those details for that person"
            else:
                return personDetails[type]

>>> telObj = Directory()
>>> telObj.AddDetail("Damion", "HomeTel", 666)
>>> telObj.RetrieveDetail("Damion", "Mobile")
Sorry, do not have those details for that person
>>> telObj.RetrieveDetail("Damion", "HomeTel")
666

You can take this a further still. Instead of creating a dictionary,
you could create a new instance of an object that inherits the
dictionary class, and that way you can override the functions that add
data to, and retrieve data from the personalDetails object. This is a
little more advanced, and I guess you've got a fair bit to digest
above!

Hope that helps.

Cheers,

Wes.

On 14/11/2007, Bryan Magalski <bryan_magalski at yahoo.com> wrote:
>
> Thank you very much for the prompt reply.
>
> I believe the way you explained my inability to grasp the concept really
> helped and I think I understand it better now.  It just proves that I need
> to practice more.
>
> If you have the time, I have another question pertaining to objects.   There
> is a ton of information on the web about how to use __init__ and self when
> creating functions within an object.  I simply "aped" the code from the
> millions of examples out there without really understanding them totally,
> (maybe now I am script kiddie?).  As I really cannot digest this, can you
> help me and elaborate on the reason of their existence and when it is
> appropriate for their use?
>
>
> Again, excellent explanation and thank you very much.  I hope that I am not
> burdening you by bombarding you with these questions.
>
>
> --Bryan
>
>
> Wesley Brooks <wesbrooks at gmail.com> wrote:
> 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 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
> > 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
> >
> >
> >
>
>
>  ________________________________
> Be a better pen pal. Text or chat with friends inside Yahoo! Mail. See how.


      ____________________________________________________________________________________
Be a better pen pal. 
Text or chat with friends inside Yahoo! Mail. See how.  http://overview.mail.yahoo.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071115/0f0eb17a/attachment-0001.htm 


More information about the Tutor mailing list