origin = (0, 0) # 定义原点
legal_x = [-100, 100] # 限定X轴的移动范围
legal_y = [-100, 100] # 限定y轴的移动范围
def create(pos_x=0, pos_y=0):
def moving(direction, step): # 定义方向和步数,
nonlocal pos_x, pos_y
new_x = pos_x + direction * step
new_y = pos_y + direction * step
if new_x < legal_x:
pos_x = legal_x - (new_x - legal_x) # 小于x轴范围时
elif new_x > legal_x:
pos_x = legal_x - (new_x - legal_x) # 大于x轴范围时
else:
pos_x = new_x
if new_y < legal_y:
pos_y = legal_y - (new_y - legal_y) # 小于y轴范围时
elif new_y > legal_y:
pos_y = legal_y - (new_y - legal_y) # 大于y轴范围时
else:
pos_y = new_y
return pos_x, pos_y
return moving fishcyou 发表于 2022-5-2 17:44
接下来输入方向位置就可以玩了:
move = create()
print("准备移动,现在的位置为:", move(, 0))
>>>
准备移动,现在的位置为: (0, 0)
print("开始移动,移动后的位置为:", move(, 10))
>>>
开始移动,移动后的位置为: (0, 10)
print("开始移动,移动后的位置为:", move(, 120))
>>>
开始移动,移动后的位置为: (0, 70) origin = (0, 0)
legal_x = (-100, 100)
legal_y = (-100, 100)
def create(pos_x=0, pos_y=0):
def moving(direction, step):
nonlocal pos_x, pos_y
new_x = pos_x + direction * step
new_y = pos_y + direction * step
if new_x < legal_x:
pos_x = legal_x - (new_x - legal_x)
elif new_x > legal_x:
pos_x = legal_x - (new_x - legal_x)
else:
pos_x = new_x
if new_y < legal_y:
pos_y = legal_y - (new_y - legal_y)
elif new_y > legal_y:
pos_y = legal_y - (new_y - legal_y)
else:
pos_y = new_y
return pos_x, pos_y
return moving
def isDirec(char):
if char == "上":
direc =
elif char == "下":
direc =
elif char == "左":
direc = [-1, 0]
elif char == "右":
direc =
elif char == "左上":
direc = [-1, 1]
elif char == "左下":
direc = [-1, -1]
elif char == "右上":
direc =
elif char == "右下":
direc =
return direc
sologan = True
b = input("开始游戏(y/n):")
while b != 'y' and b != 'n':
b = input("开始游戏(y/n):")
if b == 'n':
sologan = False
a = create()
while sologan:
char = input("请输入方向('0'退出):")
while char== "" or char not in ["上", "下", "左", "右", "左上", "左下", "右上", "右下", '0']:
char = input("请输入方向('0'退出):")
if char == '0':
break
direc = isDirec(char)
stepp = input("请输入步数(数字):")
while not stepp.isdigit():
stepp = input("请输入步数(数字):")
stepp = int(stepp)
print(f"向{char}移动{stepp}后,位置是{a(direc, stepp)}") yixinwenxin 发表于 2022-2-13 20:18
闭包会被释放吗?
会,对象在失去引用后由垃圾回收机制负责释放。 感觉闭包比较关键的一步就是要return 内部函数名 闭包怎么才能重新清零 闭包真的很妙啊
滴滴滴~打卡{:10_298:} 不太好理解啊,个人感觉是内部函数调用外部函数变量,而这个变量不会与全局变量互相影响,因此是相当于闭包起来了{:10_275:} def funX(x):
def funy(y):
return x*y
return funy
temp = funX(8)
temp(5)
求大神解答,如何得出是40,有逻辑图么? vis黄昏 发表于 2022-10-25 16:48
def funX(x):
def funy(y):
return x*y
temp = funX(8) = funy #此时将参数 x=8 传入函数funy中,得函数 funy() = 8y
temp(5) = funy(5) #此时将参参数 y= 5传入函数funy中,得 return 8*5,既40 Learning...{:10_249:} 想知道原理,为什么会这样,只记住了形式,对于其内容还是不理解,总是忍不住去想{:10_261:} {:10_269:}问答题答案:
动动手答案:
知道怎么调用,还是想不通原理
{:10_298:}嘀嘀嘀 打卡
页:
[1]
2