Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 未返回/刷新子例程中的值_Python_Return - Fatal编程技术网

Python 未返回/刷新子例程中的值

Python 未返回/刷新子例程中的值,python,return,Python,Return,我创建了一个程序,要求玩家移动,然后玩家在网格内移动。我还尝试验证输入,以便玩家不会超出网格的限制,并为此调用请求玩家移动的函数,以便他们可以继续输入移动,直到移动有效。但是,当再次调用该函数时,x_move和y_move不会被更新,这意味着用户会不断被要求重新输入他们的输入,即使输入现在是有效的。我认为这与返回值x_move和y_move有关,因为当我在接受用户的新输入后打印它们时,它们保持不变? 我的代码如下: def get_move(): advice = 'Please en

我创建了一个程序,要求玩家移动,然后玩家在网格内移动。我还尝试验证输入,以便玩家不会超出网格的限制,并为此调用请求玩家移动的函数,以便他们可以继续输入移动,直到移动有效。但是,当再次调用该函数时,x_move和y_move不会被更新,这意味着用户会不断被要求重新输入他们的输入,即使输入现在是有效的。我认为这与返回值x_move和y_move有关,因为当我在接受用户的新输入后打印它们时,它们保持不变? 我的代码如下:

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

我认为这与x_move和y_move变量的范围有关。它们在
get\u move()
中声明并赋值,但在此之前在
update\u board()
中引用

我会尝试在while语句之前添加
x\u-move,y\u-move=get\u-move()
update\u-board()
,并将while语句中的调用更改为
get\u-move()


此外,这稍微超出了问题的范围,但我会尝试重写while循环,这样您就有了一个同时检查所有四个条件的循环,否则您可能会得到以下结果:

  • 用户输入的值超出y的界限
  • 首先,在触发循环时,代码要求输入新值
  • 用户输入可接受的y值,但超出x的界限值
  • 第一个循环退出时,由不可接受的x值触发的第二个循环请求新值
  • 用户输入可接受的x值,但不可接受的y值
  • 第二,当循环退出时,代码继续执行超出范围的y值

  • 我认为这与x_move和y_move变量的范围有关。它们在
    get\u move()
    中声明并赋值,但在此之前在
    update\u board()
    中引用

    我会尝试在while语句之前添加
    x\u-move,y\u-move=get\u-move()
    update\u-board()
    ,并将while语句中的调用更改为
    get\u-move()


    此外,这稍微超出了问题的范围,但我会尝试重写while循环,这样您就有了一个同时检查所有四个条件的循环,否则您可能会得到以下结果:

  • 用户输入的值超出y的界限
  • 首先,在触发循环时,代码要求输入新值
  • 用户输入可接受的y值,但超出x的界限值
  • 第一个循环退出时,由不可接受的x值触发的第二个循环请求新值
  • 用户输入可接受的x值,但不可接受的y值
  • 第二,当循环退出时,代码继续执行超出范围的y值

  • 您需要将输出从
    get\u move()
    传递到
    x\u move
    y\u move
    ,即
    x\u move,y\u move=get\u move()
    您需要将输出从
    get\u move()
    传递到
    x\u move
    y\u move
    x\u move,y\u move=get\u move()

    我有点困惑,因为我在while语句中的调用是
    get\u move()
    ?对,但应该是
    x\u move,y\u move=get\u move()
    。当您在
    get\u move()
    函数中定义
    x\u move
    y\u move
    时,解释器只在其内存中保留名称,直到函数完成。相反,当
    get\u move()
    返回这两个数字时,上述更改将把它们传递给新变量。再次调用新变量x_move和y_move最简单。啊哈,谢谢你的解释和链接-我想我现在明白了!但是,通过在while循环之前调用x_move,y_move=get_move(),它现在可以访问x_move和y-move变量,或者在用户输入正确时调用get_move(),如果它进入while循环(即,如果输入的x或y值超出范围),它应该只调用两次
    get_move(),或者如果你在其他地方调用了它,而不是在原始帖子中显示的代码中。我有点困惑,因为我在while语句中的调用是
    get\u move()
    ?对,但它应该是
    x\u move,y\u move=get\u move()
    。当您在
    get\u move()
    函数中定义
    x\u move
    y\u move
    时,解释器只在其内存中保留名称,直到函数完成。相反,当
    get\u move()
    返回这两个数字时,上述更改将把它们传递给新变量。再次调用新变量x_move和y_move最简单。啊哈,谢谢你的解释和链接-我想我现在明白了!但是,通过在while循环之前调用x_move,y_move=get_move(),它现在可以访问x_move和y-move变量,或者在用户输入正确时调用get_move(),如果它进入while循环(即,如果输入的x或y值超出范围),它应该只调用两次
    get_move(),或者,如果您在其他地方调用了它,那么它不在原始帖子中显示的代码中。