Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7257 > unrolled thread

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

Started byEthan Furman <ethan@stoneleaf.us>
First post2011-06-08 13:43 -0700
Last post2011-06-08 13:43 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Of Functions, Objects, and Methods-I NEED HELP PLEASE Ethan Furman <ethan@stoneleaf.us> - 2011-06-08 13:43 -0700

#7257 — Re: Of Functions, Objects, and Methods-I NEED HELP PLEASE

FromEthan Furman <ethan@stoneleaf.us>
Date2011-06-08 13:43 -0700
SubjectRe: Of Functions, Objects, and Methods-I NEED HELP PLEASE
Message-ID<mailman.33.1307565054.11593.python-list@python.org>
Cathy James wrote:
> I am almost there, but I need a little help:
> 
> I would like to
> 
> a) print my dogs in the format  index. name: breed as follows:
> 
> 0. Mimi:Poodle
> 1.Sunny: Beagle
> 2. Bunny: German Shepard
> I am getting
> 
> (0, ('Mimi', 'Poodle')) . Mimi : Poodle instead-what have I done wrong?
> 
> b) I would like to append to my list, but my line dogs.dogAppend() is
> giving a TypeError:
> 
> for i in enumerate (self.dogAppend()):
> TypeError: 'list' object is not callable
> 
> Any help?
> 
> #MY CODE BELOW:
> 
> import sys
> class Dog():
>     def __init__(self, name, breed):
>         self.name = name
>         self.breed = breed
> 
>     def dogAppend(self):
>         self.dogAppend = []
>         self.dogAppend.append((self.name,self.breed))
>         return self.dogAppend
> 
> 
>     def display (self):
>         for i in enumerate (self.dogAppend()):
>             print (i,".",  self.name, ": " + self.breed)
> 
> if __name__ == "__main__":
>     dogs = Dog(name=input (" Enter Dog Name: "), breed=input ("Enter
> Dog Breed: "))
>     while not dogs:
>         print("Goodbye!!")
>         sys.exit()
>     else:
>         #dogs.dogAppend()
>         dogs.display()

 From the looks of Dog it should only hold one animal, so I would drop 
dogAppend and use a normal list; I would also change display to __str__ 
and remove the enumerate portion (since it's only one animal):

8<----------------------------------------------------------------------
#python 3
class Dog():
     def __init__(self, name, breed):
         self.name = name
         self.breed = breed

     def __str__(self):
         return self.name + ": " + self.breed

if __name__ == "__main__":
     dogs = []
     while True:
         name=input ("Enter Dog Name: ").rstrip() # stupid newlines
         breed=input ("Enter Dog Breed: ").rstrip()
         if name and breed:
             dogs.append(Dog(name, breed))
             for i, dog in enumerate(dogs):
                 print("%2i. %s" % (i, dog))
         else:
             print("good-bye")
             break
8<----------------------------------------------------------------------

~Ethan~

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web