[英]Values from subroutine not being returned/refreshed python
I have created a program that asks for the player's movement, and then the player moves within the grid. 我创建了一个要求玩家移动的程序,然后玩家在网格内移动。 I have also attempted to validate the input so that the player does not move beyond the limits of the grid, and to do so it calls the function that asks for the player's move, so that they can continue entering their move until it is a valid move.
我还尝试验证输入,以使玩家不会移动超出网格的限制,并且这样做会调用要求玩家移动的函数,以便他们可以继续输入自己的移动,直到有效为止移动。 However, the x_move and y_move are not being updated when the function is called again, meaning the user is continuously asked to re-enter their input even when it is now valid.
但是,再次调用该函数时,x_move和y_move不会更新,这意味着即使现在有效,也会不断要求用户重新输入其输入。 I think it's to do with the return of the values x_move and y_move as when I printed them after taking the user's new input, they stayed the same?
我认为这与值x_move和y_move的返回有关,因为当我在接受用户的新输入后打印它们时,它们保持不变吗? My code is below:
我的代码如下:
def get_move():
advice = 'Please enter your move in two integers, vertical, then horizontal, separated by a space. Use positive numbers for up and right, negative for down and left.'
example = 'For example, an input of \'2 2\' would be 2 moves vertically, and 2 moves horizontally.'
#Here I am joining the two inputs for ease of input for the user and so that the input is easier to work with than two serparate ones. Not sure how this will work yet with validation...
move = input(advice + example)
coor=move.split()
while len(coor)!=2:
print('Invalid input- too many or too few co-ordinates')
print('')
advice = 'Please enter your move in two integers, vertical, then horizontal, separated by a space. Use positive numbers for up and right, negative for down and left.'
example = 'For example, an input of \'2 2\' would be 2 moves vertically, and 2 moves horizontally.'
#Here I am joining the two inputs for ease of input for the user and so that the input is easier to work with than two serparate ones. Not sure how this will work yet with validation...
move = input(advice + example)
coor=move.split()
#Here I am splitting the input using whitespace, hence why nothing is in the bracket
move = move.split()
#Now I am declaring the variables that are assigned to the split move input
y_move,x_move=move
#And now assigning them as integers
x_move = int(x_move)
y_move = int(y_move)
return x_move, y_move
def update_board(b, current_y_loc, current_x_loc):
while (-1>= current_y_loc-y_move) or (current_y_loc-y_move>size_of_grid):
print('INVALID INPUT')
get_move()
while (-1>= current_x_loc+x_move) or (current_x_loc+x_move>size_of_grid):
print('INVALID INPUT')
get_move()
b[current_y_loc-y_move][current_x_loc+x_move] = player_location
current_y_loc -= y_move
current_x_loc += x_move
print("current location = ", current_x_loc, current_y_loc)
return current_y_loc, current_x_loc
您实际上需要将输出从get_move()
传递到x_move
和y_move
即x_move, y_move = get_move()
I think it's to do with the scope of the x_move and y_move variables. 我认为这与x_move和y_move变量的范围有关。 They're declared and assigned values in
get_move()
, but then referenced in update_board()
before that. 在
get_move()
声明和分配了它们的值,但在update_board()
之前在update_board()
中update_board()
进行了引用。
I'd try adding x_move,y_move=get_move()
to update_board()
, before the while statements, and changing the call to get_move()
inside the while statements to match. 我会尝试在while语句之前将
x_move,y_move=get_move()
到update_board()
,然后将while语句中对get_move()
的调用更改为匹配。
Additionally, this is slightly outside the scope of the question, but I'd try to rewrite the while loops so that you instead have one loop that checks all four conditions at the same time, or else you could end up with the following outcome: 此外,这稍微超出了问题的范围,但是我会尝试重写while循环,以便您可以使用一个循环同时检查所有四个条件,否则可能会得到以下结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.