Reputation: 655
All I want to do is have my loop use the distance formula to move the turtle down every second. However, every time I try and use the formula this error comes up. Does anyone know where I am going wrong?
import turtle
wn = turtle.Screen()
tony = turtle.Turtle()
tony.shape("turtle")
tony.pensize(5)
tony.up()
tony.left(90)
tony.forward(100)
tony.left(180)
tony.down()
gravity= float(10.3)
for i in [1,2,3,4,5,6,7,8,9,10]:
tony.stamp()
time = range(1,10,1)
distance = float((gravity/2)*((time**2)-((time-1)**2)))
tony.forward(distance)
Upvotes: 0
Views: 2206
Reputation: 2743
The range
function returns a list. You are storing this list in time
, and then attempting to use it as the base of time ** 2
(time squared). Perhaps you wanted to set time to the value of i
?
Upvotes: 1
Reputation: 8627
Your variable time = range(1, 10, 1)
is equivalent to time = [1, 2, 3, 4, 5, 6, 7, 8, 9]
and so trying to square a list, as in distance = float((gravity/2)*((time**2)-((time-1)**2)))
is undefined.
If your intention was to square each element in the list (i.e. [1, 4, 9, 16, ...]
then what you're trying to do is formally called a map. Fortunately, python has a builtin map.
f = lambda x: x**2
map(f, list)
Above I used a lambda, which is an inline function. you could just have (slightly less) easily (slightly more inline with PEP8):
def f(x):
return x ** 2
map(f, list)
Also: You subtracted 1 from your list as well. I'll leave it up to you to figure out a solution to that using the tools above.
Upvotes: 0