2014-12-14 2 views
0

나는이 문제가있는 오류가 발생합니다. 나는 방울이라고 불리는 함수를 쓸 필요가있다. 물방울을 그려 넣는다. 매개 변수는 방울을 그리는 거북이, 크기 (각 방울의 직경) 및 간격 (목록이며 각 방울 사이의 거리에 사용됨)입니다. 파이썬 버전 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

당신이 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) 
관련 문제