我们的任务是创建一个网格网格,并使用一个游标(+)移动(上,下,右,左)响应命令。我已经完成了网格和功能,但是我无法使我的移动功能正常工作,并且无法显示更新的网格。我需要代码看起来像的例子。
这是我的代码:
x = y = 0
size = int(input('Enter grid size: '))
print(f'Current location: ({x},{y})')
def show_grid(x, y):
for i in range(size):
for j in range(size):
if i == y and j == x:
print('+', end=' ')
else:
print('.', end=' ')
print()
show_grid(x,y)
def show_menu():
print('-- Navigation --')
print('2 : Down')
print('8 : Up')
print('6 : Right')
print('4 : Left')
print('5 : Reset')
print('0 : EXIT')
return 0
show_menu()
opt = int(input('Select an option: '))
def move(x, y, opt):
while True:
option = show_menu()
if option == 0:
print(f'Current location: ({x},{y})')
break #exit
elif option == 2:
return x-1, y
elif option == 8:
return x+1, y
elif option == 4:
return x, y-1
elif option == 6:
return x, y+1
else:
x, y = move(x, y, option)
print(f'Current: ({x},{y})')
if 0 <= x < size and 0 <= y < size:
show_grid(x,y)
else: #game over
print('The new location is off the board.')
break
print('Exit the program')
move(x, y, opt) ```
在move()
的定义内,您正在使用return
(即退出函数),然后转到调用show_grid()
的部分