Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.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
LPTHW ex45 Python if/else行为-退出/循环并返回所需内容_Python_Loops_If Statement - Fatal编程技术网

LPTHW ex45 Python if/else行为-退出/循环并返回所需内容

LPTHW ex45 Python if/else行为-退出/循环并返回所需内容,python,loops,if-statement,Python,Loops,If Statement,注意:我在这里搜索并阅读了一些与if/else循环和类似内容相关的帖子。不确定我是否有知识/背景来翻译那里的答案 我正在做LPTHW,并在Ex45之后为自己制作了一个简单的游戏。 这是摘录- #Here I'm importing the moduels that I need for exit and randint. from sys import exit from random import randint #Define a parent class called Scene cla

注意:我在这里搜索并阅读了一些与if/else循环和类似内容相关的帖子。不确定我是否有知识/背景来翻译那里的答案

我正在做LPTHW,并在Ex45之后为自己制作了一个简单的游戏。 这是摘录-

#Here I'm importing the moduels that I need for exit and randint.
from sys import exit
from random import randint

#Define a parent class called Scene
class Scene(object):

    def enter (self):
        print "This scene is not yet configured. Subclass it and"
        print "implement enter()"
        exit(1)

# Create class Engine, theres a while loop here too.
# Basically, it takes the opening scene as an "anchor" of sorts
# And everytime it checks to make sure that the next scene is not 'Finished',
# in which case its the last_scene.
# To understand this code, we need to look at the bottom of the file.
# There is a Map class with scenes dict defined and various methods for this.

class Engine(object):

    def __init__(self, scene_map):
      self.scene_map = scene_map

    def play(self):
      current_scene = self.scene_map.opening_scene()
      last_scene = self.scene_map.next_scene('Finished')

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

       # be sure to print out the last scene
       current_scene.enter()
以下是我似乎有问题的对象:

class Hallway(Scene):

    def enter(self):
        print "You are now in the long hallway"
        print "unfortunately, it is not very bright and its pretty hard to see"
        print "you hear the sound of footsteps running towards you - its the killer!"
        print "What do you do - use your Knife or Run?"
        decision = raw_input("Knife or Run? > ")

        if decision == "Knife":
            print "You pull your knife from your back pocket and swing it through the air"
            print "You hear a scream and then a thud on the floor"
            print "You find the light and turn it on, the killer is dead"
            return 'finished'

        elif decision == "Run":
            print "You run as fast as you can but you just can't get away"
            return 'death'

        else:
            print "You have two choices here, please try again!"
            return 'hallway'




class Map(object):

    scenes = {
        'respawn' : Respawn(),
        'kitchen' : Kitchen(),
        'hallway' : Hallway(),
        'death' : Death(),
        'finished' : Finished(),
        'won' : Won(),
    }

    def __init__(self, start_scene):
        self.start_scene = start_scene

    def next_scene(self, scene_name):
        val = Map.scenes.get(scene_name)
        return val

    def opening_scene(self):
        return self.next_scene(self.start_scene)

a_map = Map('respawn')
a_game = Engine(a_map)
a_game.play()
当我运行程序时,它从不考虑if/else循环。 看看它是如何在这里再次运行的-

LPTHW$ python ex45_mygame.py
You've respawned, would you like to start from Hallway or Kitchen?
> Hallway
You are now in the long hallway
unfortunately, it is not very bright and its pretty hard to see
you hear the sound of footsteps running towards you - its the killer!
What do you do - use your Knife or Run?
Knife or Run? > Knife
You pull your knife from your back pocket and swing it through the air
You hear a scream and then a thud on the floor
You find the light and turn it on, the killer is dead
You are now in the long hallway
unfortunately, it is not very bright and its pretty hard to see
you hear the sound of footsteps running towards you - its the killer!
What do you do - use your Knife or Run?
Knife or Run? > Knife
You pull your knife from your back pocket and swing it through the air
You hear a scream and then a thud on the floor
You find the light and turn it on, the killer is dead
You won! Good job.





Would you like to play again? Y/N?
谁能帮我理解我做错了什么。我相信这很简单——只需要推一下。对不起,我是新手


这就是问题的症结所在,问题在于引擎类中的缩进错误。最后一行应该在循环之外;否则,它将始终被调用,因此每个场景执行两次

如果对while循环使用适当的2或4空格缩进,问题会更容易看出,就像对其他块一样

以下是正确的代码:

def play(self):
    current_scene = self.scene_map.opening_scene()
    last_scene = self.scene_map.next_scene('Finished')

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

    # be sure to print out the last scene 
    current_scene.enter()

我能想到的唯一一件事是,您正在检查Engine.play中的Finished,但从场景返回Finished。如果你让它们都完成了,它会工作吗?如果我定义另一个名为WIN的对象并返回它,那么它会做同样的事情。下面是我更改它之后发生的事情-我看到了发生的事情-在不同的机器之间移动。vimrc配置引用制表符间距。谢谢你的帮助!如果答案对你有帮助,别忘了接受。