2017-01-20 5 views
0

タートルを別のタートルに移動する必要があるプログラムを作成しています。しかし、単純な:それはother_turtle.pos()メソッド間法と減算され、浮いが定義されていないことを言うのでカメとother_turtleは2匹のすでに定義されてカメは動作しませんタートルを別のタートルに移動する

ある

turtle.goto(other_turtle.pos()) 

。だから私は試し続けてother_turtle.pos()float(other_turtle.pos())に変更しました。それから、あなたは方法を浮かべることができないと言って、それは間違っていました。

私の質問:これは可能なのですか、これは不可能ですか、またはパイソンカメ自体の限界のために不可能ですか?

コード:

def turtle_clone(clone_nmb): 
    return [turtle.Turtle() for i in range(clone_nmb)] 

def straight_curve(turtle,number_of_arms,number_of_points,colors): 
    turtles = turtle_clone(2) 
    turtles[0].hideturtle() 
    turtles[1].hideturtle() 
    turtles[0].color(colors[0]) 
    turtles[1].color(colors[0]) 
    pos = 0 
    for i in range(number_of_arms): 
     turtles[0].setheading(pos) 
     turtles[0].forward(5) 
     turtles[1].setheading(pos+(360/number_of_arms)) 
     turtles[1].forward(5*number_of_points) 
     turtles[1].right(180) 
     for i in range(number_of_points): 
      turtles[0] 
      turtle.penup() 
      turtle.goto(turtles[0].xcor,turtles[0].ycor) 
      turtle.pendown() 
      turtle.goto(turtles[1].xcor,turtles[1].ycor) 
      turtles[0].forward(5) 
      turtles[1].forward(5) 
     pos += 360/number_of_arms 
     turtles[0].goto(0,0) 

エラーメッセージ:

Traceback (most recent call last): 
    File "C:\Users\gularson\Documents\Garrett\Messing Around\turtle_functions.py", line 97, in <module> 
    straight_curve(turt,5,30,["white"]) 
    File "C:\Users\gularson\Documents\Garrett\Messing Around\turtle_functions.py", line 87, in straight_curve 
    turtle.goto(turtles[0].xcor,turtles[0].ycor) 
    File "C:\Users\gularson\Documents\Garrett\Python\lib\turtle.py", line 1776, in goto 
    self._goto(Vec2D(x, y)) 
    File "C:\Users\gularson\Documents\Garrett\Python\lib\turtle.py", line 3165, in _goto 
    diff = (end-start) 
    File "C:\Users\gularson\Documents\Garrett\Python\lib\turtle.py", line 262, in __sub__ 
    return Vec2D(self[0]-other[0], self[1]-other[1]) 
TypeError: unsupported operand type(s) for -: 'method' and 'float' 
+0

常に問題になるFULLエラーメッセージ(トレースバック) - あなたの説明よりも便利です。 – furas

+0

あなたは 'pos()'で '()'を使用していますか?あなたの説明から、 'turtle.goto(other_turtle.pos)'を '()'なしで使うように見えます。完全なエラーメッセージとコードを表示してください。 – furas

答えて

0

の式:

turtle.goto(other_turtle.pos()) 

が作業を行います。エラーなしで、次の実行:

import turtle 

other_turtle = turtle.Turtle() 

turtle.goto(other_turtle.pos()) 

turtle.done() 

しかし、もちろん、それは非常に興味深いものではありませんが(0)すでに(0、0)で、他のカメの上に、(0でカメを移動し、それは法的です。)だから、何か別のものがあなたを踏み外さなければなりません。あなたが実際にあなたのコードに書いた何

です:あなたがそれらを呼び出すのではなく、方法xcorとを渡しているとして、あなたが報告されたエラーが発生します

turtle.goto(other_turtle.xcor, other_turtle.ycor) 

turtle.goto(turtles[0].xcor,turtles[0].ycor) 

。それはされている必要があります。

turtle.goto(other_turtle.xcor(), other_turtle.ycor()) 

turtle.goto(turtles[0].xcor(), turtles[0].ycor()) 

か、について尋ねた元のコード:

turtle.goto(other_turtle.pos()) 

turtle.goto(turtles[0].pos()) 

がうまく働いているだろう。

+0

名前で動いているカメを指定しましたが、動いているものは名前がなく、リストに定義されていました。 –

+0

@GarrettGularson、あなたのコードでエラーを見るのに十分なコンテキストがあるので、私は自分の答えを更新しました。 – cdlane

+0

ありがとう、それは正常に働いた –

関連する問題