更新:网格PYTHON [英] UPDATE: Mesh grid PYTHON
问题描述
我正在处理一个网格网格,该网格具有一个光标,当您输入分配的数字时该光标会移动.我能够使光标移动,唯一的问题是我希望它在光标移动时打印出更新后的坐标的位置(例如,如果光标向下移动一个块,则新位置应是(0,-1)).
I'm working on a mesh grid that has a cursor that moves when you enter the assigned number. I was able to get the cursor to move, the only problem I'm having is that I want it to print out the location of the updated coordinates as the cursor moves, (ex. if the cursor moves down one block the new location should be (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')
推荐答案
在定义move()
的内部,您要先使用return
(即,退出函数),然后再调用show_grid()
Inside your definition of move()
, you are using return
(ie, exiting the function) before you get to the part that calls show_grid()
无需返回,只需将x,y设置为所需的值即可.另外,我注意到另一件事可能会导致您出现问题.您使用option
决定下一步,然后使用option = show_menu()
.但是,您定义show_menu()
的方式始终返回0.为了使options
包含用户的输入,您应该更改show_menu()
的定义方式,或者更改option
的分配方式.
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.
在OP更新后进行 这是我看到的问题
Edit after OP's update: Here are the problems that I see
- 在功能
show_menu()
中:您从未要求用户提供任何输入.您总是返回0
. - 在功能
move()
中:x
和y
未更新.您会将更新后的x
和y
传递给show_grid()
,但此后将不使用它们. - 您当前在
#main program
中是break
.
- In your function
show_menu()
: you never asked for any input from the user. You are always returning0
. - In your function
move()
:x
andy
are not updated. You're passing the updatedx
andy
toshow_grid()
, but after that they are not used. - You currently
break
out of your#main program
ifchoice == 0
.
这是您要解决我上面提到的每个问题的工作:
Here's what you'll have to do to fix each of the problems I mentioned above:
- 在函数
show_menu()
中:要求用户输入并返回而不是0. - 将您当前传递给
show_grid()
和y
的新值返回. - 删除
break
.如果您没有先固定#1就这样做,则会陷入无限循环-但是如果先固定#1,它将等待用户输入.
- In your function
show_menu()
: ask for the user's input and return it instead of 0. - Return the new values of
x
andy
that you are currently passing toshow_grid()
. - Remove
break
. If you do this without fixing #1 first you'll end up in an infinite loop - but if you fix #1 first it will wait for user input.
这篇关于更新:网格PYTHON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!