Python Turtle – How do I stop the turtle at a specific distance or coordinate?
Question:
This is my attempt to make the turtle stop after traveling nearly 400 pixels.
def race():
while True:
alex.forward(r_alex)
a = a + r_alex
if a > 399.9:
break
And this is what I got back
UnboundLocalError: local variable 'a' referenced before assignment
Answers:
The line a = a + r_alex
uses a
before you actually define a
.
I’m guessing a
is the turtle’s displacement so perhaps you should try the following:
def race():
a = 0
while True:
alex.forward(r_alex)
a += r_alex
if a > 399.9:
break
Even better:
def race():
a = 0
while(a > 399.9):
alex.forward(r_alex)
a += r_alex
This is my attempt to make the turtle stop after traveling nearly 400 pixels.
def race():
while True:
alex.forward(r_alex)
a = a + r_alex
if a > 399.9:
break
And this is what I got back
UnboundLocalError: local variable 'a' referenced before assignment
The line a = a + r_alex
uses a
before you actually define a
.
I’m guessing a
is the turtle’s displacement so perhaps you should try the following:
def race():
a = 0
while True:
alex.forward(r_alex)
a += r_alex
if a > 399.9:
break
Even better:
def race():
a = 0
while(a > 399.9):
alex.forward(r_alex)
a += r_alex