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


Groups > comp.lang.python > #7257

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ethan@stoneleaf.us>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'help?': 0.03; 'sys': 0.05; '%s"': 0.09; '__name__': 0.09; 'append': 0.09; 'callable': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:gator410.hostgator.com': 0.09; '~ethan~': 0.09; 'def': 0.12; 'wrote:': 0.14; '"))': 0.16; '"__main__":': 0.16; '#python': 0.16; '(0,': 0.16; '(self):': 0.16; 'list;': 0.16; 'newlines': 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; 'received:gateway13.websitewelcome.com': 0.16; 'subject:Objects': 0.16; 'subject:PLEASE': 0.16; 'cc:addr:python-list': 0.17; 'header :In-Reply-To:1': 0.21; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'wrong?': 0.23; 'code': 0.24; 'object': 0.26; 'import': 0.29; 'subject:HELP': 0.29; 'class': 0.29; 'cc:addr:python.org': 0.30; '(since': 0.30; 'typeerror:': 0.30; 'looks': 0.31; 'print': 0.31; 'done': 0.32; 'break': 0.33; 'follows:': 0.34; 'skip:# 10': 0.34; 'header:User-Agent:1': 0.35; 'hold': 0.36; 'change': 0.37; 'but': 0.38; 'portion': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'list,': 0.39; 'getting': 0.40; 'format': 0.40; 'almost': 0.60; 'subject:, ': 0.60; 'received:websitewelcome.com': 0.67; 'below:': 0.79; 'dogs': 0.84; 'stupid': 0.84; 'subject:Methods': 0.84
Date Wed, 08 Jun 2011 13:43:48 -0700
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Thunderbird 1.5.0.10 (Windows/20070221)
MIME-Version 1.0
To Cathy James <nambo4jb@gmail.com>
Subject Re: Of Functions, Objects, and Methods-I NEED HELP PLEASE
References <BANLkTimNb3r31sQOZ8Go8qbppg9-T6HsBg@mail.gmail.com>
In-Reply-To <BANLkTimNb3r31sQOZ8Go8qbppg9-T6HsBg@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - gator410.hostgator.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - stoneleaf.us
X-BWhitelist no
X-Source
X-Source-Args
X-Source-Dir
X-Source-Sender mail.admailinc.com ([192.168.10.136]) [72.11.125.166]:1343
X-Source-Auth ethan+stoneleaf.us
X-Email-Count 3
X-Source-Cap dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ==
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.33.1307565054.11593.python-list@python.org> (permalink)
Lines 79
NNTP-Posting-Host 82.94.164.166
X-Trace 1307565054 news.xs4all.nl 49182 [::ffff:82.94.164.166]:49200
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7257

Show key headers only | View raw


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~

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web