如何在特定距离或坐标处停止Python中的乌龟?

时间:2015-04-11 06:43:56

标签: python distance turtle-graphics 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

1 个答案:

答案 0 :(得分:2)

在您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