我的if和else声明不会相互绕过

时间:2016-03-02 01:27:09

标签: python

我们在课堂上的工作是编写一个基于文本的互动游戏。我基于斯坦利的比喻,这是一个真正的视频游戏。问题是当if和else语句被绕过时,我收到一个错误。重点不是每次都要问每一个问题。它应该只按玩家选择的顺序询问它们。这是我的代码。我想知道是否有办法解决所有问题。 附:我没有完成一些if和else语句所以我只是告诉他们打印鹅。

这是我的代码:

print ("Stanley lives in a world where he is always told what to do. He goes        to work, he pushes the buttons that he is told to push over his intercom, and then    he goes home and eats the food that he is given by the overseer. One day, stanley goes to work and discovers that nobody is there. He waits for commands to come over on his intercom but they fail to do so.")
begin = input ("does stanley leave his office?")
if begin == "yes":
    q2 = input ("Stanley left his office and decided to go to the break room to see where everybody went, it was the first decision he ever made and it made him feel proud. Stanley got to the break room but nobody was there. He had noticed that in the break room, there were two doors that he had never seen before. Which door did he choose, right or left?")

else: 
    print ("So stanley stayed in his office where he never recieved another command, never recieved anymore food and died of starvation and depression.")

if q2 == "left":
    q3 = input("Stanley decided to walk throught the left door where he found a red door and a blue door, but which color did he choose?")

else:
    q4 = input ("Stanley decided to go through the right door which brought him into a room with a green door and an orange door. Which did he choose?")

if q3 == "blue":
    q5 = input ("So after a little bit of puzzling over, Stanley decided to go through the blue door where found an elevator and a gun. Does he pick up the gun.")

else:
    print ("So Stanley walked throught the red door where a gunsman opened the door and shot him, he fell to the ground and died.")

if q5 == "yes":
    q9 = input ("Stanley wondered what this gun was doing here and he knew that things were getting pretty strange. He though to himself that in the case that his work mates had become zombies and were hiding amongst this maze of doors, he could protect himself. Now that he has a gun, does he decide to use the elevator?")



if q4 == "orange":
    print ("Stanley decided to go through the orange door. As he was walking through the hallway beyond the orange door, the floor beneath him collapsed which led to his demise.")

else:
    q6 = input ("Stanley decided to go through the green door where he was confronted with a narrow hallway. He continued to walk through the hallway until he came to a split in which there were two branches of this hallway for him to choose. Did he choose the left or the right?")

if q9 == "yes":
    q7 = input ("Stanley decided to use the elevator. He got into the elevator and there were two buttons. Up or Down?")

else:
    print("Goose")

if q7 == "down":
    q8 = input ("Stanley decided to do down the elevator. He waited until he had reached the bottom and the doors had opened up. The elevator doors had opened to a huge room full of chairs. It was almost as if a meeting were to be held there but nobody showed up. All of the sudden stanley hears some noise. He sees one of his workmates run out from underneath one of the chairs and start running toward him. His workmates name if Philip. Philip is covered in blood and has a missing arm. Does Stanley shoot philip? ")

else:
    print ("Stanley just killed someone for the first time and he is going into shock. He curls up on the ground where a creature named Zinyak greets him and tosses him around like a ragdoll until he is dead.")

if q8 == "no":
    q10 = input("Philip runs to you, he starts telling you about how there had been a huge creature unlike any creature he had ever seen. He said his name was Zinyak and that he claimed to rule the zen empire. Philip told Zinyak that earth was not part of the zen empire and that he needed to leave him alone. Zinyak tore his arm off and threw him across the room which apparently isn't too far from where stanley is right now. Does Stanley choose to confront Zinyak?")

else:
    print("Goose")

if q10 == "yes":
    q11 = input ("So stanley takes off his shirt and ties it tightly over philips arm. He then goes through the door where he sees Zinyak. Zinyak begins to charge at him. Does Stanley shoot zinyak?")

else:
    print("Goose")

if q5 == "no":
    print ("Stanley reached for his gun only to find that he had never picked it up and now he must run. Zinyak being about 10 feet tall, easily catches up to Stanley and rips him limb from limb.")

else:
    print("Stanley decides to shoot Zinyak. He empties his entire clip on Zinyak but it doesn't even seem to bother him, its as if Zinyaks Skin is made of Armor. Zinyak than grabs Stanley tries to run away but it seems as if he cannot move. He is than awoken by his wife telling he had a nightmare.")

3 个答案:

答案 0 :(得分:1)

您希望将if个支票嵌套在彼此之内。提出问题后,将相关的if / else块缩进4个空格。

例如,在if q2问题之后立即进行q2 = ...检查。

begin = input ("does stanley leave his office?")
if begin == "yes":
    q2 = input ("Stanley left his office and decided to go to the break room to see where everybody went, it was the first decision he ever made and it made him feel proud. Stanley got to the break room but nobody was there. He had noticed that in the break room, there were two doors that he had never seen before. Which door did he choose, right or left?")

    if q2 == "left":
        q3 = input("Stanley decided to walk throught the left door where he found a red door and a blue door, but which color did he choose?")

    else:
        q4 = input ("Stanley decided to go through the right door which brought him into a room with a green door and an orange door. Which did he choose?")

else: 
    print ("So stanley stayed in his office where he never recieved another command, never recieved anymore food and died of starvation and depression.")

每个问题都会继续。将if q3放在q3 = ...之后,依此类推。您将不得不继续添加缩进级别。这是预期的。

答案 1 :(得分:0)

你可以指定所有q ??输入前的变量。

q1=''
q2=''
... 

例如,如果q9回答'no',q7没有初始化,如果q7 == down;将抛出错误,初始化变量可以防止这种情况。

if q9 == "yes":
    q7 = input ("Stanley decided to use the elevator. He got into the elevator and there were two buttons. Up or Down?")

else:
    print("Goose")

if q7 == "down":
    q8 = input ("Stanley decided to do down the elevator. He waited until he had reached the bottom and the doors had opened up. The elevator doors had opened to a huge room full of chairs. It was almost as if a meeting were to be held there but nobody showed up. All of the sudden stanley hears some noise. He sees one of his workmates run out from underneath one of the chairs and start running toward him. His workmates name if Philip. Philip is covered in blood and has a missing arm. Does Stanley shoot philip? ")

答案 2 :(得分:0)

你没有解释你想要什么,或者你得到了什么错误。但是,我已经编写和/或调试得足够多,我相信我理解你的范例。

您需要添加变量。现在,保持简单;让它成为一个数字或简单的标签,表明玩家所在的房间。然后你的中心代码看起来像这样:

alive = True
while alive:
    # Print the text you get on entering the room
    print room_script[state]
    # Ask for an action decision
    reply = input (room_question[state])
    # Move to the next room based on answer
    if reply.lower[0] = 'y':
        state = move_on_yes[state]
    else:
        state = move_on_no[state]
    # Did the decision end the game?
    alive = room_is_safe[state]

现在,您使用游戏脚本和过渡填充列表

  • room_script [n] 进入房间#n时要打印的文字。 (字符串)
  • room_question [n] 在房间#n询问的问题。 (字符串)
  • move_on_yes / no [n] 在室内#n时,指示在“是”/“否”上移动到哪个房间。 (整数)
  • room_is_safe [n] 布尔句:这个房间会让玩家活着吗?

例如,一个非常简单的游戏可能如下所示:

room_script = [
    "You are in the first room.",
    "Good choice!  You win!",
    "Did you really think that was going to work?"
]
room_question = [
    "There is a lethal-looking potion here.  Do you drink it?",
    "Do you want to start over?",
    "Do you want to start over?"
]
move_on_yes = [2, 0, -1]
move_on_no  = [1, -1, -1]
room_is_safe = [True, True, False]

从这里开始,你真的需要从其他人那里阅读和玩类似的简单游戏。这是一个有趣的类型,但对于一个成熟的游戏,有一个很多的考虑因素。