我的if和else语句不会绕过彼此

2024-09-27 17:32:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我们在课堂上的工作是写一个基于文本的互动游戏。我是根据斯坦利寓言,这是一个真正的电子游戏我的。问题是当if和else语句被绕过时,我会得到一个错误。关键不是每次都要问每个问题。它应该只按玩家选择的顺序问他们。这是我的密码。我想知道有没有办法把所有的问题都解决掉。 另外,我没有完成一些if和else语句,所以我只是告诉他们打印goose。你知道吗

这是我的密码:

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.")

Tags: andthetoinputifthatelsehe
3条回答

你可以分配所有的q??输入前的变量。你知道吗

q1=''
q2=''
... 

例如,如果q9回答“否”,而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? ")

您需要将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 = ...之后,依此类推。你必须不断增加缩进的层次。这是意料之中的。你知道吗

你没有解释你想要什么,或者你犯了什么错误。但是,我已经编写和/或调试了足够的代码,我相信我理解您的范例。你知道吗

您需要添加一个状态变量。现在,让它保持简单;让它是一个数字或简单的标签,表示播放器所在的房间。你的中心代码是这样的:

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]

现在,用游戏脚本和转换填充列表

  • 房间#脚本[n]输入房间#n时要打印的文本(字符串)
  • 房间问题[n]要在房间中提问的问题(string)
  • 移动到#是/否[n]在房间#n中时,表示要移动到哪个房间,并显示“是”/“否”。(整数)
  • 房间是安全的[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]

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

相关问题 更多 >

    热门问题