37讲课后题乌龟吃鱼
import random as rlevel_x =
level_y =
class Turtle:
inibody = 100
iniX = r.randint(level_x,level_x)
iniY = r.randint(level_y,level_y)
new_X = 0+iniX
new_Y = 0+iniY
def move(self):
direction = []##我想要实现去哪个方向,所以这里加了一个方向的列表
dire=-1
direcion.append(r.choice(['x','y']))
dire += 1
if direction == 'x':
new_X = new_X+r.choice()
if new_X >level_x:
new_X = level_x-(new_X-level_x)
if new_X < level_x:
new_X = level_x+(level_x-new_X)
if direction == 'y':
new_Y = new_Y+r.choice()
if new_Y >level_y:
new_Y = level_y-(new_Y-level_y)
if new_Y < level_y:
new_Y = level_y+(level_y-new_Y)
newbody = self.inibody - 1
print('乌龟的当前位置为%s,%s.体力为%s'%(new_X,new_Y,newbody))
但是运行后报错说NameError: name 'direction' is not defined,我十分疑惑,求大佬救救孩子吧
拼写错误direcion,改成direction 冬雪雪冬 发表于 2020-10-13 17:02
拼写错误direcion,改成direction
谢谢,被自己蠢哭了
页:
[1]