Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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_Variables_Global - Fatal编程技术网

Python全局变量干扰?

Python全局变量干扰?,python,variables,global,Python,Variables,Global,所以基本上这是我正在做的一个小弦乐游戏的一部分,这真的让我很兴奋。玩家可以按“a”或“d”向左或向右查看房间以收集线索。如果玩家已经在那个地方看过了。它弹出一条信息说“你已经来过这里” 虽然,它会转到正确的点一次,然后下一点会弹出一条消息,说“你已经到过这里”。例子。如果我按“a”,那么“d”代表“d”,它会说我已经去过那里了,但我还没有去过。有什么帮助/建议吗 left = 0 right = 0 #Room 1 movements def roomOneLeft(): global

所以基本上这是我正在做的一个小弦乐游戏的一部分,这真的让我很兴奋。玩家可以按“a”或“d”向左或向右查看房间以收集线索。如果玩家已经在那个地方看过了。它弹出一条信息说“你已经来过这里”

虽然,它会转到正确的点一次,然后下一点会弹出一条消息,说“你已经到过这里”。例子。如果我按“a”,那么“d”代表“d”,它会说我已经去过那里了,但我还没有去过。有什么帮助/建议吗

left = 0
right = 0
#Room 1 movements
def roomOneLeft():
    global left
    left = 1
    print '-- You search the rubble and find some cloth'
    return roomOneMoves()

def roomOneRight():
    global right
    right = 1
    print '-- You find a pick under a wooden chair'
    return roomOneMoves()

#room 1 user choose
def roomOneMoves():
    global left
    global right
    move = raw_input("")
    if left == 1:
        print 'you have already been here'
        return roomOneMoves()
    if right == 1:
        print 'you have already been here'
        return roomOneMoves() 
    if move == 'a':
        roomOneLeft()     
    if move == 'd':
        roomOneRight()

roomOneMoves()

编辑:谢谢大家的帮助。如果我的代码看起来很令人沮丧,我很抱歉!现在开始工作了。但是只有你的帮助。

虽然有很多事情可以说你的代码是“不确定的”,但是你的问题来自这样一个事实,即无论玩家做出什么动作,
都会被检查。如果按“a”
左键
将变为1。在它之后按“d”将检查
left
是否为1并打印消息

尽管有很多事情可以说你的代码是“不确定的”,但你的问题来自这样一个事实,即无论玩家做出什么动作,左
和右
都会被检查。如果按“a”
左键
将变为1。在它之后按“d”将检查
left
是否为1并打印消息

问题在于您的
roomOneMoves
功能:

move = raw_input("")
首先,你从用户那里得到方向;但你还没有对它进行评估

if left == 1:
    print 'you have already been here'
    return roomOneMoves()
if right == 1:
    print 'you have already been here'
    return roomOneMoves() 
您检查用户以前是否向左或向右移动过并显示消息,更糟糕的是,您再次启动
roomOneMoves

if move == 'a':
    roomOneLeft()     
if move == 'd':
    roomOneRight()
只有在这之后,你才能真正评估新的方向

您应该首先计算新方向,并且仅在计算时检查该方向是否在之前输入过一次:

move = raw_input("")
if move == 'a':
    if left == 1:
        print 'you have already been here'
    else:
        roomOneLeft()
elif move == 'd':
    if right == 1:
        print 'you have already been here'
    else:
        roomOneRight()
roomOneMoves()

此外,您可能希望在结束时无条件地重新启动该方法。

问题在于您的
roomOneMoves
函数:

move = raw_input("")
首先,你从用户那里得到方向;但你还没有对它进行评估

if left == 1:
    print 'you have already been here'
    return roomOneMoves()
if right == 1:
    print 'you have already been here'
    return roomOneMoves() 
您检查用户以前是否向左或向右移动过并显示消息,更糟糕的是,您再次启动
roomOneMoves

if move == 'a':
    roomOneLeft()     
if move == 'd':
    roomOneRight()
只有在这之后,你才能真正评估新的方向

您应该首先计算新方向,并且仅在计算时检查该方向是否在之前输入过一次:

move = raw_input("")
if move == 'a':
    if left == 1:
        print 'you have already been here'
    else:
        roomOneLeft()
elif move == 'd':
    if right == 1:
        print 'you have already been here'
    else:
        roomOneRight()
roomOneMoves()

此外,您可能希望在结束时无条件地重新启动该方法。

您不会检查您的移动,直到为时已晚。注意:

move = raw_input("")
if left == 1:
    print 'you have already been here'
    return roomOneMoves()
if right == 1:
    print 'you have already been here'
    return roomOneMoves() 
if move == 'a':
    roomOneLeft()     
if move == 'd':
    roomOneRight()
如果用户实际向左走,您只想计算
left==1
部分,不是吗?相反,嵌套if语句,如下所示:

move = raw_input("")
if move == 'a':
    if left:
        print 'you have already been here'
        return roomOneMoves()
    roomOneLeft()
if move == 'd':
    if right:
        print 'you have already been here'
        return roomOneMoves() 
    roomOneRight()

此外,您应该重新考虑您的数据结构,使用具有布尔值或类似值的字典,而不是全局变量。此外,由于大多数Python解释器无法有效地进行计算,因此您可能希望使用迭代的主循环,而不是递归地调用下一个函数。

除非为时已晚,否则不会检查移动。注意:

move = raw_input("")
if left == 1:
    print 'you have already been here'
    return roomOneMoves()
if right == 1:
    print 'you have already been here'
    return roomOneMoves() 
if move == 'a':
    roomOneLeft()     
if move == 'd':
    roomOneRight()
如果用户实际向左走,您只想计算
left==1
部分,不是吗?相反,嵌套if语句,如下所示:

move = raw_input("")
if move == 'a':
    if left:
        print 'you have already been here'
        return roomOneMoves()
    roomOneLeft()
if move == 'd':
    if right:
        print 'you have already been here'
        return roomOneMoves() 
    roomOneRight()
此外,您应该重新考虑您的数据结构,使用具有布尔值或类似值的字典,而不是全局变量。此外,由于大多数Python解释器无法有效地进行计算,您可能希望使用迭代的主循环,而不是递归地调用下一个函数。

浏览代码:

  • 我们称之为
    roomOneMoves()
  • 用户按下
    a
    ,移动变为
    'a'
  • left
    不是
    1
    ,所以我们不输入if的那个分支
  • right
    不是
    1
    ,所以我们不输入if的那个分支
  • move
    'a'
    所以我们称之为
    roomOneLeft()
  • left
    变为
    1
    ,我们打印一条消息,然后再次调用
    roomOneMoves()
  • 用户按
    d
    move
    变为
    'd'
  • left
    现在是1,所以我们打印一条消息并再次调用
    roomOneMoves()
  • 换句话说,在您决定用户真正想要去哪个房间之前,您正在检查您是否去过房间并离开。

    浏览代码:

  • 我们称之为
    roomOneMoves()
  • 用户按下
    a
    ,移动变为
    'a'
  • left
    不是
    1
    ,所以我们不输入if的那个分支
  • right
    不是
    1
    ,所以我们不输入if的那个分支
  • move
    'a'
    所以我们称之为
    roomOneLeft()
  • left
    变为
    1
    ,我们打印一条消息,然后再次调用
    roomOneMoves()
  • 用户按
    d
    move
    变为
    'd'
  • left
    现在是1,所以我们打印一条消息并再次调用
    roomOneMoves()

  • 换句话说,在您决定用户真正想要去哪个房间之前,您正在检查您是否去过房间并离开。

    对于仅指定这两种状态的变量,您应该真正使用布尔值(
    True
    False
    )。仍然没有运气=[按另一个键只会显示“you have before here”(你已经在这里了)伙计,你所有的
    return
    语句都是递归的-如果有人按
    a
    2000次,他将达到最大递归深度。请阅读事件循环…@user1947560布尔值注释不应该解决你的问题(这本来是一个答案)但请评论一下您应该改进什么,以使您的意图更加明确。除了观察到的bug,请仔细看看
    roomOneMoves