1

私は次のコードを持っています。コードの最後の関数は、子供を母親に集める必要があります。

class MotherTurtle(Turtle):  
    def __init__(self, home):
        Turtle.__init__(self, home)
        self.children = []
        self.home = home
        self.setName("Mum")
        
    def giveBirth(self, name):
        newborn = Turtle(self.home)
        newborn.setName (name)
        self.children.append(newborn)
        return newborn
        
    def greetChildren(self):
        for child in self.children:
            print "Hi %s" %(child.name)
                
    def gatherChildren(self):
        for child in self.children:
            child.moveTo(self.home)

私は子供たちを母親に集める必要があります。

ここに画像の説明を入力してください

これは、プログラムを実行したときに発生するエラーです。

======= Loading Progam =======
>>> world = makeWorld()
>>> mum = MotherTurtle(world)
>>> mary = mum.giveBirth("Mary")
>>> jimmy = mum.giveBirth("Jimmy")
>>> mum.greetChildren()
Hi Mary
Hi Jimmy
>>> mary.turn(-45)
>>> mary.forward(120)
>>> jimmy.turn(90)
>>> jimmy.forward()
>>> mum.gatherChildren()

エラーは次のとおりです。

'list'オブジェクトに属性がありません'moveTo'属性が見つかりません。存在しないオブジェクトの一部にアクセスしようとしています。C:\ Users \ user \ Desktop\159171の21行目を確認してください

4

1 に答える 1

0

方法は次のとおりです。

def gatherChildren(self):
    
    # get the position of the mother turtle    
    mumX = self.getXPos()
    mumY = self.getYPos()
    # use an offset so not all turtles are on top of each other
    spacing = 10
    offset = spacing
    # loop through the list of children to place each child close to the mother
    for child in self.children:
      child.moveTo(mumX + offset, mumY + offset)
      offset = offset + spacing
于 2012-05-28T12:46:36.297 に答える