如何在特定距离或坐标处停止 Python 中的乌龟?
How do I stop the turtle in Python at a specific distance or coordinate?
这是我试图让乌龟在行驶了将近 400 像素后停下来的尝试。
def race():
while True:
alex.forward(r_alex)
a = a + r_alex
if a > 399.9:
break
这就是我得到的结果
Traceback (most recent call last):
File "/Users/Jason/Desktop/Assignment1/a1_s3536647_bui_anh_quan.py", line 84, in <module>
race()
File "/Users/Jason/Desktop/Assignment1/a1_s3536647_bui_anh_quan.py", line 77, in race
a = a + r_alex
UnboundLocalError: local variable 'a' referenced before assignment
行 a = a + r_alex
在你实际定义 a
之前使用 a
。
我猜 a
是乌龟的位移,所以也许您应该尝试以下操作:
def race():
a = 0
while True:
alex.forward(r_alex)
a += r_alex
if a > 399.9:
break
更好:
def race():
a = 0
while(a > 399.9):
alex.forward(r_alex)
a += r_alex
这是我试图让乌龟在行驶了将近 400 像素后停下来的尝试。
def race():
while True:
alex.forward(r_alex)
a = a + r_alex
if a > 399.9:
break
这就是我得到的结果
Traceback (most recent call last):
File "/Users/Jason/Desktop/Assignment1/a1_s3536647_bui_anh_quan.py", line 84, in <module>
race()
File "/Users/Jason/Desktop/Assignment1/a1_s3536647_bui_anh_quan.py", line 77, in race
a = a + r_alex
UnboundLocalError: local variable 'a' referenced before assignment
行 a = a + r_alex
在你实际定义 a
之前使用 a
。
我猜 a
是乌龟的位移,所以也许您应该尝试以下操作:
def race():
a = 0
while True:
alex.forward(r_alex)
a += r_alex
if a > 399.9:
break
更好:
def race():
a = 0
while(a > 399.9):
alex.forward(r_alex)
a += r_alex