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

C

Cathy James

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()
 
J

John Gordon

In said:
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
def dogAppend(self):
self.dogAppend = []

You have a method and a list that are both called dogAppend. Try naming
one of them something different.
 
E

Ethan Furman

Larry said:
else does not belong with while.

else works just fine with while; it is the path taken when the while is
exhausted, but not broken out of:

--> i = 5
--> while i:
.... print(i)
.... i -= 1
.... else:
.... print("blast off!")
....
5
4
3
2
1
blast off!


--> i = 5
--> while i:
.... print(i)
.... i -= 1
.... if i == 3:
.... print('aborting')
.... break
.... else:
.... print("blast off!")
....
5
4
aborting

~Ethan~
 
E

Ethan Furman

Ethan said:
else works just fine with while; it is the path taken when the while is
exhausted, but not broken out of:

It works with 'for' as well.

~Ethan~
 
D

Dennis Lee Bieber

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

2: This won't work to append to your list anyway... each time you call dogAppend() it will
clear out any existing list and result in a list of one element, a tuple of (name, breed). You
can do exactly the same thing with a single line: return [(self.name, self.breed)]
Worse than that... The first time dogAppend is called on an instance
of Dog, it replaces the dogAppend method with a binding to the (single
element) list created by the method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top