如何解决这个龟功能?

时间:2014-12-14 13:19:08

标签: python list python-3.x turtle-graphics

我遇到这个问题我遇到了错误。我需要写一个叫做水滴的函数 画出一系列水滴。参数是t乌龟绘制液滴,尺寸(每个液滴的直径)和分离(这是一个列表,它用于每个液滴之间的距离)。我使用Python 3.4.2版。我得到的错误是TypeError。这是代码:

def droplets(t, size, separations):
    for i in range(len(separations)):
        t.down()
        t.circle(size)
        t.up()
        t.forward([separations * i])

import turtle
turt = turtle.Turtle()
droplets(turt, 5, [20, 25, 30])

错误:

Traceback (most recent call last):
  File "C:/Python34/My Python Program/Midterm 2 PP Test.py", line 10, in <module>
    droplets(turt, 5, [20, 25, 30])
  File "C:/Python34/My Python Program/Midterm 2 PP Test.py", line 6, in droplets
    t.forward([separations * i])
  File "C:\Python34\lib\turtle.py", line 1636, in forward
    self._go(distance)
  File "C:\Python34\lib\turtle.py", line 1603, in _go
    ende = self._position + self._orient * distance
  File "C:\Python34\lib\turtle.py", line 257, in __mul__
    return Vec2D(self[0]*other, self[1]*other)
TypeError: can't multiply sequence by non-int of type 'float'

1 个答案:

答案 0 :(得分:1)

如果您想迭代separations

def droplets(t, size, separations):
    for i in range(len(separations)):
        t.down()
        t.circle(size)
        t.up()
        t.forward(separations[i])

或者,更简单地说,只需迭代separations而不是使用索引:

def droplets(t, size, separations):
    for separation in separations:
        t.down()
        t.circle(size)
        t.up()
        t.forward(separation)