首页 > 解决方案 > 无法让我的冒险游戏正常运行(选择不起作用)

问题描述

我目前正在使用python创建一个僵尸冒险游戏或一个项目,我已经全部写出来了但是有些选择不起作用,我尝试使用else语句和elif语句仍然无法正常工作,任何人都可以看到问题是什么?

这是代码:

print("Welcome to the zombie apocalypse game, Your name is Dion")
print("the apocalypse has been going on for the past year and a half")
print("you have been separated from your group and must make your way to the air field by making the right choices")
print("make the wrong choices and you will loose the game and die.")
print("some choices may seem correct at the time but will lead you down a path of death")
print("Lets Hope You Can Make It!")





answer = input ("Would you like to play the game? (yes/no)")

if answer.lower().strip() == "yes":

    answer = input("you are walking down a street, you reach a crossroad, would you like to go left or right? (left/right)").lower().strip()

if answer == "left":
        answer = input("you walking into a mugger, Do you fight or run? (fight/run)")

        if answer == "fight":
            print("Poor choice you were stabbed, YOU LOSE!!")

elif answer == "run":

        answer = input("nice choice, you ran away from the mugger, you now come to another split in the road, are you going left or right?? (left/right)")

        if answer == "left":
            print("you run into a large group of zombies,You have nothing to fight them off with and they eat you. You Loose")

elif answer == "right":

        answer = input("In the distance you see a fire and some tents, you can either go to the tents and see what happens or try to find your own shelter (tents/yourself)")

        if answer == "tents":
            print("You make your way upto the tents, stepping on a booby trap left the zombies, a spike goes through your foot and your scream attracts a heard of zombies and they kill you and all of the poeple in the tents")


else:

        answer = input("you find a small bulding that is empty, you decide to make camp here for the night, in the back you notice a generator, do you try and start it or leave it and sleep in the cold and dark? (start/leave)")

        if answer == "start":
            print("you have no idea what youre doing and pull the wrong leaver causing the generator to explode, killing you in the process. You loose")

        else:
            print("you stay in the cold and dark, during the night a small group of zombies made their way into the builiding with you as you didnt check for open doors or other entry points and they attack you in your sleep, you have now become a zombie. You Lose the game")




elif answer == "right":

        answer = input("you contiue down the path for a while longer, You come across a dead man, all beaten and with bite marks, you see that he has a gun, Do you take it or leave it? (take/leave)")

        if answer == "take":

            print("Stupid choice that was a zombie and now you have been bitten and now youre dead")

elif answer == "leave":

        answer = input("You continue down the path and see a large swarm of zombies you can either fight them or try and go through a building to escape them (fight/building")

        if answer == "fight":
            print("Pretty Stupid choice, theyre zombies and youre ... well youre you, and youre dead")


elif answer == "building":

        answer = input("okay were in the building, theres a zipwire on the roof or you can look for supplies? (zipewire/supplies)")

        if answer == "zipewire":
            print("you ran into a group of mainiacs and they have captured you and made you a slave, you lose!!")


elif answer == "supplies":

        answer = input("nice, you found food, clothes, a gun and keys to a car, are you going yo risk unlocking the car and making noise which could alert the zombies or are you going to find another way? (unlock/other)")

        if answer == "other":
            print("Poor choice there is to many of them and now youre dead")

elif answer == "unlock":

        answer = input("okay you got to the car and it has a full tank of fuel, its only a few hour drive to the air feild, do you drive there now in the dark or wait till the morning? (morning/now)")

        if answer == "morning":
            print("nope, the zombies are surrounding you and destory the car and you inside it, you lost")


elif answer == "now":
            print("you made it to the air strip and meet up with your friends, youre safe and you win the game!!!!")



else:
     print ("fine dont play :(")

如果您选择左作为第一个选项,然后运行游戏就会停止,当它应该提供更多选项和选择以在游戏中进行时,它只是没有发生,我不知道为什么。请有人帮忙。

标签: python

解决方案


您的代码只是响应您的输入,然后一直运行到最后。没有什么可以让它回到起点,所以你只需要一两个答案。

这是作业吗?我假设是这样,只是给你一个提示。尝试while循环。这就是他们的样子。

exit = False

while not exit:
    answer = input("What do you do?")

    if "exit" in answer.lower():
        exit = True

推荐阅读