Of Functions, Objects, and Methods-I NEED HELP PLEASE

Larry Hudson orgnut at yahoo.com
Fri Jun 10 02:07:55 EDT 2011


On 06/08/2011 11:59 PM, Larry Hudson wrote:
> On 06/08/2011 01:09 PM, Cathy James wrote:
>> I am almost there, but I need a little help:
>>
>> I would like to
>>
... <deleted text>

> Here's one possible replacement. There are many other approaches as well.
> (This leaves the individual dogs as a (name, breed) tuple. It could be modified for other
> definitions of a dog. -- Exercise left for the reader...) ;-)
>
... <more deleted text>

In thinking about this some more, I thought a dictionary instead of a list would be a better fit 
for this example.  For one thing, it allows accessing the dogs by name instead of an arbitrary 
index number.  But remember that a dictionary is unordered -- it won't be displayed in the same 
order that the entries were made.

Here's this approach, rewritten (and slightly expanded) using a dictionary.

class DogKennel:
     def __init__(self):
         """Dog names/breeds kept in a dictionary"""
         self.dogs = {}

     def addDog(self):
         """Add a single dog to the dictionary"""
         name = input("Enter dog's name:  ")
         if name == "":      #   Abort with empty input
             return False
         breed = input("Enter dog's breed: ")
         if breed == "":     #   Abort here if needed also
             return False
         self.dogs[name] = breed
         return True

     def makeKennel(self):
         """Add multiple dogs (a pack?) to the dictionary"""
         while self.addDog():
             pass

     def getDog(self, name=""):
         """Get the dog's breed by its name"""
         if name in self.dogs:
             return self.dogs[name]
         else:
             return None

     def display(self):
         """Display all the dogs in the kennel (the dictionary)"""
         i = 1
         for dog in self.dogs.keys():
             print("%2d.  %s: %s" % (i, dog, self.dogs[dog]))
             i += 1

#   Note this is a normal function, NOT a member function of DogKennel.
#   It probably should go in the __main__ section to keep it separate from
#   the DogKennel class.  (This would be significant only if you're going
#   to import the DogKennel class into other programs.)
def yesno(prompt = ""):
     """Get a yes or no answer.  Returns True if yes, False if no"""
     if prompt != "":
         prompt = prompt + " (y/n)  "
     while True:
         ans = input(prompt).upper()
         if ans != "":           #   Answer is not empty
             if ans[0] == 'Y':   #   1st char is 'Y'?
                 return True
             if ans[0] == 'N':   #   1st char is 'N'?
                 return False
             #   Otherwise loop back for another go

if __name__ == "__main__":
     dogs = DogKennel()
     dogs.makeKennel()
     dogs.display()
     if yesno("Add more dogs?"):
         dogs.makeKennel()
         print("The kennel now contains")
         dogs.display()
     while True:
         name = input("Which dog do you want?  ")
         if name == "":
             break
         breed = dogs.getDog(name)
         if breed != None:
             print(name, "is a", breed)

#---------------
I hope studying this (and my previous) examples help you understand things better.
Keep at it...  It will sink in with a little effort.     :-)
I also hope my rather verbose answers give you a little insight about the sort of things you 
need to consider when designing your programs.

      -=- Larry -=-



More information about the Python-list mailing list