Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.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_While Loop_Indentation - Fatal编程技术网

Python 而循环缩进正在干扰我的变量

Python 而循环缩进正在干扰我的变量,python,while-loop,indentation,Python,While Loop,Indentation,我对编程非常陌生,我有一段代码可以用一种方式工作,但在另外两种方式中只能半正确地工作。我只是想知道为什么。 这是我正在关注的一个文本游戏: while current_scene != last_scene: next_scene_name = current_scene.enter() current_scene = self.scene_map.next_scene(next_scene_name) current_scene.enter() 这段代码工作

我对编程非常陌生,我有一段代码可以用一种方式工作,但在另外两种方式中只能半正确地工作。我只是想知道为什么。 这是我正在关注的一个文本游戏:

while current_scene != last_scene:
        next_scene_name = current_scene.enter()
        current_scene = self.scene_map.next_scene(next_scene_name)

current_scene.enter()

这段代码工作正常,但是,我认为缩进current_scene.enter将使其成为while循环的一部分,如下所示

while current_scene != last_scene:
    next_scene_name = current_scene.enter()
    current_scene = self.scene_map.next_scene(next_scene_name)

    current_scene.enter()
在我的代码的其他部分中,场景包含输入。当这一行缩进成为while循环的一部分时,所发生的是当您给出正确的输入时,它再次穿过同一场景。第二次给它正确的输入最终允许它继续游戏的其余部分。为什么会发生这种情况

最后,如果我将current_scene.enter移动到while循环上方,它会在正确输入后重复第一个场景两次,这是为什么? 下面是它的样子

current_scene.enter()
while current_scene != last_scene:
    next_scene_name = current_scene.enter()
    current_scene = self.scene_map.next_scene(next_scene_name)

我感谢所有的帮助。这是我关于StackOverflow的第一个问题,如果我做得不正确,我很抱歉。

它看起来像是当前的\u场景。enter返回下一个场景名称,您需要调用当前的\u场景。在最后一个场景中输入以滚动积分

while current_scene != last_scene:
        current_scene = self.scene_map.next_scene(current_scene.enter())

current_scene.enter()

如果没有额外的变量,逻辑可能是有意义的。虽然额外的变量可能是必需的,因为场景映射。下一个场景。。。可能不会像current_scene.enter那样返回下一个场景名称。

但是,我认为缩进current_scene.enter将使其成为while循环的一部分-它将使其成为while循环的一部分,但是否适当?不。该调用不应该在循环内。在第二个循环中,您正在执行当前的_场景。请输入while循环的每个迭代。不确定你想要的行为是什么。