【发布时间】:2020-03-07 03:54:42
【问题描述】:
我正在研究一个网格,当您输入分配的数字时,它的光标会移动。我能够让光标移动,我遇到的唯一问题是我希望它在光标移动时打印出更新坐标的位置,(例如,如果光标向下移动一个块,新位置应该为 (0,-1))。
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()
choice = int(input('Enter an option: ')) ####current location not updating
def move(x, y, choice):
if choice == 2: # down
show_grid(x, y+1)
elif choice == 8: # up
show_grid(x, y-1)
elif choice == 4: # left
show_grid(x-1, y)
elif choice == 6: # right
show_grid(x+1, y)
elif choice == 5: # reset to (0,0)
show_grid(x, y)
elif choice == 1:
print(choice, 'Not a valid input. Try again.')
show_grid(x, y)
elif choice == 3:
print(choice, 'Not a valid input. Try again.')
show_grid(x, y)
elif choice == 7:
print(choice, 'Not a valid input. Try again.')
show_grid(x, y)
elif choice == 9:
print(choice, 'Not a valid input. Try again.')
show_grid(x, y)
move(x, y, choice)
#main program
while True:
choice = show_menu()
if choice == 0:
print(f'Current location: ({x},{y})')
break
else:
x,y = move(x,y,choice)
print(f'Current location: ({x},{y})')
if 0 <= x < size and 0 <= y < size: # inside the board
print(f'Current location: ({x},{y})')
else: # outside the board
print('The new location is off the board.')
break
print('Exit the program')
【问题讨论】:
-
你能提供更多细节吗?您提供什么输入,程序应该做什么,以及它在做什么?
-
棋盘是点网格(如上图),光标是(+)号,功能应该是:按2(移动+向下1),按8(移动上),按 4(向左移动),按 6(向右移动),按 0(退出),按 5(将 + 重置为 (0,0))。
-
我有这些功能,但是当我运行它时,它什么也不做,只是再次打印菜单。