【问题标题】:UPDATE: Mesh grid PYTHON更新:网状网格 PYTHON
【发布时间】: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))。
  • 我有这些功能,但是当我运行它时,它什么也不做,只是再次打印菜单。

标签: python loops grid


【解决方案1】:

move() 的定义中,在到达调用show_grid() 的部分之前,您正在使用return(即退出函数)

编辑:无需返回,只需将 x, y 设置为所需的任何值。另外,我注意到另一件事可能会导致您出现问题。您使用option 来决定下一步做什么,并使用option = show_menu()。但是您定义show_menu() 的方式总是返回0。为了使options 包含用户的输入,您应该更改show_menu() 的定义方式,或者更改option 的分配方式。

在 OP 更新后编辑: 以下是我看到的问题

  1. 在您的函数show_menu() 中:您从未要求用户提供任何输入。你总是返回0
  2. 在您的函数move()xy 未更新。您将更新后的xy 传递给show_grid(),但之后它们就不再使用了。
  3. 如果choice == 0,您当前的break 不在您的#main program 中。

要解决我上面提到的每个问题,您必须执行以下操作:

  1. 在您的函数show_menu() 中:要求用户输入并返回它而不是 0。
  2. 返回您当前传递给show_grid()xy 的新值。
  3. 删除break。如果您在没有先修复 #1 的情况下执行此操作,您最终会陷入无限循环 - 但如果您先修复 #1,它将等待用户输入。

【讨论】:

  • 哦,好吧,我明白了,有什么更好的选择?
  • 我已经编辑了答案以添加一些可能对您有所帮助的信息。
  • 谢谢,我可以让“+”移动,现在唯一的问题是坐标的位置没有随着 x 和 y 移动而更新?
  • 我根据您的更新添加了更多信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-15
  • 2019-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多