class Toy(object):

def play(self, toy):
''''Print squeak'''
print 'Squeak!'

class Dog(object):

def __int__(self, name):
'''Name a dog'''

self.name = name

def call(self, shout):
'''Return true if shout is exactly 'Here, n!', otherwise return False'''

return shout == 'Here, ' + str(self.name) + '!'

def play(self, toy, n):
'''Print 'Yip! ' followed by the output from toy.play on the same line.\
This happens n times and outputs are in separate lines. if n is \
negative, then n is treated as 0'''

if n < 0:
n = 0
for i in range(n):
t = Toy()
print 'Yip! ' + str(t.play(toy)),

There is a problem in the play function of Dog class.
The result are expected to be:
Yip! Squeak!
Yip! Squeak!
Yip! Squeak!

but the result I got is:
Squeak!
Yip! None Squeak!
Yip! None Squeak!
Yip! None

can anybody tell me what's wrong with my code?


Thx a lot :)

t.play(toy) doesn't have a return, which means it returns None, so
print 'Yip! ' + str(t.play(toy))
prints "Yip!None"

Try

print 'Yip!',
t.play(toy)
#
# or
class Toy(object):
    def play(self, toy, lit):
        '''Print squeak'''
        print lit+'Squeak!'

t.play(toy, 'YIP!')  ## note that 'toy' is not used anywhere
#
# or
class Toy(object):
    def play(self, toy):
        '''Print squeak'''
        return 'Squeak!'

print 'Yip! ' + t.play(toy)

Edited 6 Years Ago by woooee: n/a

Sorry, please use code tags around your Python code!

Run this code:

print("Please enclose your code in code tags like this:")
print("["+"code]")
print("... your code goes here ...")
print("[/"+"code]")

Edited 6 Years Ago by Lardmeister: n/a

This article has been dead for over six months. Start a new discussion instead.