2015-11-29 54 views
0

小蟒蛇問題,如何使龜移動根據[(160, 20), (-43, 10), (270, 8), (-43, 12)]其中第一個數字是角度轉向,第二個是行駛距離。蟒蛇列表解釋(瓦特/海龜)

我嘗試:

print('Question 11') 
import turtle 

wn = turtle.Screen() 
wn.bgcolor("hot pink") 
tess = turtle.Turtle() 
tess.shape("turtle") 
tess.color("blue") 

def path(x): 
    for a, b in len(x): # Not so sure about this line. 
     tess.forward(a) 
     tess.right(b) 
l = [(160, 20), (-43, 10), (270, 8), (-43, 12)] 
path(l) 
wn.mainloop() 

錯誤,我已經得到了:

TypeError: 'list' object cannot be interpreted as an integer

TypeError: 'int' object is not iterable

+1

'len(x)'返回序列'x'的長度。你想'爲a,b在x'。我想你想轉向前進,而不是後轉。 –

+0

謝謝!工作很好。欣賞它。 – Fizzhaz

回答

2

我不熟悉的龜,但後續行:

for a, b in len(x): # Not so sure about this line. 

這一行是錯誤的:x如下面的代碼表示一個列表。 len(x)返回一個整數,但整數不可迭代。

,你的意思是:

for a, b in x: 

,而不是你的代碼。