Move functions for Mesh grid

让人想犯罪 __ 提交于 2020-03-04 18:21:24

问题


Our assignment is to create a mesh grid, with a cursor(+) that moves(up, down, right, left) apon command. I have the grid and the fuctions done, but i'm having trouble getting my move function's to work, along with the updated grid to show. Example of what I need the code to look like.

This is my code:

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()

option = int(input('Select an option: '))
def move(x, y, option):
    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, option) ```

回答1:


Inside your definition of move(), you are using return (ie, exiting the function) before you get to the part that calls show_grid()

EDIT: instead of returning, just set x, y to whatever they need to be. Also, I noticed one other thing that may be causing you problems. You use option to decide what to do next, and option = show_menu(). But the way you've defined show_menu(), it always returns 0. In order for options to contain the user's input, you should either change the way show_menu() is defined, or change the way option is assigned.



来源:https://stackoverflow.com/questions/60510682/move-functions-for-mesh-grid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!