这是我试图让海龟在行进近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
答案 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