繁体   English   中英

部分代码未执行(未显示错误)

[英]Part of code not executing (no errors shown)

这是一个聊天机器人,其中包括一个选择您自己的冒险游戏,但是如果您选择 select 作为第一选择的第二个选项(探索海滩),它不会在游戏中执行任何操作。

当我 select 第一个选项时,一切正常,所以我不明白为什么它没有显示。 如果我将游戏与聊天机器人分开运行,游戏本身就可以正常运行,并且我已经检查了缩进。 请帮忙。

import random


#Choose YOur Own Adventure Game ========================================


print("Would you like to play a Choose Your Own Adventure game?\n")

game1 = input("Play a Choose YOur Own Adventure Game? ")

if (game1 == "Yes" or game1 == "yes"):
    print("Alright! Let's get started...")
    countdown = 5
    while (time > 0):
        print (("...") + str(time) + ("..."))
        time = time - 1
    print("Loading Game... 3, 2, 1..\n")
    print("Welcome player!")

    print("Hello " + name + ", welcome to this adventure game!\n")

    print("You find yourself in a strange hut, in a dazed state. \nWhere am I? you wonder.")

    print("You see lots of light near the door, & when you walk out you see... ")
    
    print("... wow, so much sand!\n")

    drown = random.randint(1,2)
    help = random.randint(1,2)

    
#============================ Story starts here ========================================================
print("Turns out you're on a beach,\nYou don't know how you got here.")
print("The messy hut isn't too far from the shore.")
print("You walk onto the white sand, it's hot,\nand it seems like there's no one around.\n")
print("Weird, so you decide to look around.")
print("Maybe you can find some info on how you got here.\n")
        
#===================================Choice 1 =====================================
print("Do you want to examine the hut for clues or explore the beach?")
print("1: Examine the hut\n2: Explore the beach\n")
explore = int(input("Examine the hut or explore the beach?"))
        
while (explore < 1 or explore > 2):
    print("Please enter 1 or 2.\n")
    explore = int(input("Examine the hut or explore the beach?"))
            
if (explore == 1):
    print("You go back into the hut.")
    print("Near the door you notice something glistening.")
    print("You walk over and decide to take a closer look.")
#========================================= Choice 2 ====================================
    print("You find a small clear vial with some mysterious liquid in it.")
    print("I should:\n1: Drink it.\n2: Just put in my pocket.\n")
    drink =int(input("Drink it or save for it later?"))
    while (drink < 1 or drink > 2):
        print("Please enter either 1 or 2.\n")
        drink =int(input("Drink it or save for it later?"))
#=========================================== Choice 3 =========================================
    if (drink == 2):
        print("You put it in your pocket.\n")
        print("There's nothing else in the hut.")
        print("You then go back onto the beach.")
        print("You go for a walk near the water.")
        print("You don't find anything interesting.")
        print("You decide to go for a swim.")
        print("The water envelopes you, and you drown.")
        print("Game Over.")
        print("This is one of the 8 endings thanks for playing!")

    if (drink == 1):    
        print("You drink the whole bottle, it made you feel weird.")
        print("All of a sudden you feel drowsy.")
        print("Do you want to take a nap to feel refreshed?")
        print("1: Yes\n2: No\n")
        nap = int(input("Want to take a nap?"))
        while (nap < 1 or nap > 2):
            print("Enter 1 or 2")
            nap = int(input("Do you want to take a nap to feel refreshed?"))
            
        if(nap == 1):
            print("You decide to doze off...\n")
            naptime =int(input("How long you want to sleep for?(in hours)"))

            if (naptime < 3):
                print("You took a short nap.\n When you woke up you decided to go relax on the beach.")
                print("The water looks cold, just what you need on a hot day like this.")
                print("You decide to go for a swim.\n")
                print("Some time later...\n")
                if (drown == 1):
                    print("While in the water,a strong current swept you away from the shore, \nyou're not a great swimmer, so you drowned.")
                    print("This is one of the 8 endings, thanks for playing!")
                if (drown == 2):
                    print("Exhausted from swimming, you lay down on the sand to rest.")
                    print("You were never good with puzzles or survival,\nit doesn't seem like theres a way to get off this island.\n")
                    print("Do you want to give up on the thought of escaping?")
                    print("1: Yes\n2: No")
                    give_up = int(input("Give up on escaping?"))
                    while (give_up < 1 or give_up > 3):
                        print("Enter 1 or 2")
                    if (give_up == 1):
                        print("You gave up on escaping the island.")
                        print("Without food or water you slowly waste away.")
                        print("You died, not knowing where you are or how you got there.\n")
                        print("This is one of the 8 endings, thanks for playing!")
                    if (give_up == 2):
                        print("You decide to keep looking for an exit.")
                        print("After a few days it feels like you've searched every nook and cranny.")
                        print("You don't find anything.")
                        print("Game Over.")
                        print("This is one of the 8 endings, thanks for playing!")





    #========================================== Ending A ================================================
            if (naptime > 3):
                print("'Hey! Get up! You've been voted off'\nyou hear a man's voice shout")
                print("(a few minutes later)\nApparently this is one of those game shows, y'know like Survivor")
                print("Watching me sleep for so long was too boring, so I have been kicked off.")
                print("This is one of the 8 endings, thanks for playing!")

        if(nap == 2):
            print("Everything became hazy, as you stagger onto the hot sand.")
            print("Everything goes black as you fall straight into the water.\n")
            print(name + " wake up! Are you alright?")
            print("Huh?  Oh... it's just an interesting dream.")
            print("Whew, what a weird nightmare... you sigh.")
            print("This is one of the 8 endings, thanks for playing!")



    #============================================ If you explore the beach  =========================================    
    if (explore == 2):
        print("You decide to go for a walk near the water.")
        print("You don't find anything out of the ordinary, it's just a beach.")
        print("Do you want to relax on the beach?")
        print("1: Yes\n2: No\n")
        relax = int(input("Relax?"))
        while (relax < 1 or relax > 2):
            print("Enter 1 or 2")
            relax = int(input("Relax?"))
        if (relax == 1):
            print("You laid down on the sand.")
            print("You fall asleep.")
            print("Thump, thump, .... THUD\n")
            print("You open your eyes,everything is shaking.")
            print("It's, it's... an earthquake!")
            print("The raggedy hut collapses, any clues that might've been there are destroyed.\n")
            print("You go examine the remains of the hut.")
            print("Within the sand you find a flare gun!")
            print("You use it, and a ship picks you up.")
            print("You've escaped!")
            print("This is one of the 8 endings, thanks for playing!")

        if (relax == 2):
            print("With nothing else to do you go back into the hut.\n")
            print("Upon further examination you see a small vial full of some liquid.")
            print("The liquid is clear and odorless.")
            print("I should:\n1: Drink it.\n2: Just put in my pocket.\n")
            drink =int(input("Drink it or save for it later?"))

            if(drink == 1):
                print("You're not sure what you just drank, but one thing's for sure,")
                print("you are probably going to faint.")
                print("Everything goes black...\n")
                print("Hey! Wake up!")
                print("You wake up to see two lifeguards standing over you.")
                print("You passed out from dehydration, and everything was just a hallucination.")
                print("This is one of the 8 endings, thanks for playing!")

            if(drink == 2):
                print("You put it in your pocket.")
                print("You are no closer to escaping than before.\n")
                print("All of a sudden you catch a glimpse of ship passing by!")
                print("Maybe if they hear you you can catch a ride off this island!\n")
                print("Do you want to call out to them?")
                print("1: Yes\n2: No\n")
                scream = int(input("Try asking for help?"))

                if(scream == 1):
                    if(help == 1):
                        print("Your screams were successful!")
                        print("You are saved!")
                        print("This is one of the 8 endings, thanks for playing!")

                    if(help == 2):
                        print("Whoever was on the ship didn't seem to hear you.")
                        print("It seems like there's nothing you can do.")
                        print("Game Over.")
                        print("This is one of the 8 endings, thanks for playing!")

                if(scream == 2):
                    print("Nobody comes to your rescue.")
                    print("You are trapped, and without any food or water you slowly perish.")
                    print("Game Over.")
                    print("This is one of the 8 endings, thanks for playing!")

                while (scream < 1 or scream > 2):
                    print("Enter 1or 2.\n")
                    scream =int(input("Try asking for help?"))



            while (drink < 1 or drink > 2):
                print("Please enter either 1 or 2.\n")
                drink =int(input("Drink it or save for it later?"))
#End of Game ===============================================================
elif (game1 == "No" or "no"):
    print("Oh, I see you don't want to play...")
    print("How sad, I spent all that time setting it up for you ;(")
    print("Oh well, I guess we won't play.\n")
    
    

您的代码的问题是您已将语句if explore == 2放入if explore == 1中。 Python 非常重视缩进。 您需要做的就是将第二个if statement与第一个 if 语句缩进。 在你的代码中。

样本:

x=2
if x==1:
    if x==2:
        body.... 

这是你的陈述样本。 这里,x=2。 首先它检查 x==1。 如果否,则转到其他语句。 但是您已经在第一个内部定义了if x==2 所以只有当第一条语句为 True 时才会执行。

所以正确的表达方式是:

x=2
if x==1:
    body....
elif x==2:
    body.... 

您的代码就是这种情况。

if (explore == 1):
    print("You go back into the hut.")
    print("Near the door you notice something glistening.")
    print("You walk over and decide to take a closer look.")
#========================================= Choice 2 ====================================
    print("You find a small clear vial with some mysterious liquid in it.")
    print("I should:\n1: Drink it.\n2: Just put in my pocket.\n")
    drink =int(input("Drink it or save for it later?"))
    while (drink < 1 or drink > 2):
        print("Please enter either 1 or 2.\n")
        drink =int(input("Drink it or save for it later?"))
#=========================================== Choice 3 =========================================
    if (drink == 2):
        print("You put it in your pocket.\n")
        print("There's nothing else in the hut.")
        print("You then go back onto the beach.")
        print("You go for a walk near the water.")
        print("You don't find anything interesting.")
        print("You decide to go for a swim.")
        print("The water envelopes you, and you drown.")
        print("Game Over.")
        print("This is one of the 8 endings thanks for playing!")

    if (drink == 1):    
        print("You drink the whole bottle, it made you feel weird.")
        print("All of a sudden you feel drowsy.")
        print("Do you want to take a nap to feel refreshed?")
        print("1: Yes\n2: No\n")
        nap = int(input("Want to take a nap?"))
        while (nap < 1 or nap > 2):
            print("Enter 1 or 2")
            nap = int(input("Do you want to take a nap to feel refreshed?"))
            
        if(nap == 1):
            print("You decide to doze off...\n")
            naptime =int(input("How long you want to sleep for?(in hours)"))

            if (naptime < 3):
                print("You took a short nap.\n When you woke up you decided to go relax on the beach.")
                print("The water looks cold, just what you need on a hot day like this.")
                print("You decide to go for a swim.\n")
                print("Some time later...\n")
                if (drown == 1):
                    print("While in the water,a strong current swept you away from the shore, \nyou're not a great swimmer, so you drowned.")
                    print("This is one of the 8 endings, thanks for playing!")
                if (drown == 2):
                    print("Exhausted from swimming, you lay down on the sand to rest.")
                    print("You were never good with puzzles or survival,\nit doesn't seem like theres a way to get off this island.\n")
                    print("Do you want to give up on the thought of escaping?")
                    print("1: Yes\n2: No")
                    give_up = int(input("Give up on escaping?"))
                    while (give_up < 1 or give_up > 3):
                        print("Enter 1 or 2")
                    if (give_up == 1):
                        print("You gave up on escaping the island.")
                        print("Without food or water you slowly waste away.")
                        print("You died, not knowing where you are or how you got there.\n")
                        print("This is one of the 8 endings, thanks for playing!")
                    if (give_up == 2):
                        print("You decide to keep looking for an exit.")
                        print("After a few days it feels like you've searched every nook and cranny.")
                        print("You don't find anything.")
                        print("Game Over.")
                        print("This is one of the 8 endings, thanks for playing!")





    #========================================== Ending A ================================================
            if (naptime > 3):
                print("'Hey! Get up! You've been voted off'\nyou hear a man's voice shout")
                print("(a few minutes later)\nApparently this is one of those game shows, y'know like Survivor")
                print("Watching me sleep for so long was too boring, so I have been kicked off.")
                print("This is one of the 8 endings, thanks for playing!")

        if(nap == 2):
            print("Everything became hazy, as you stagger onto the hot sand.")
            print("Everything goes black as you fall straight into the water.\n")
            print(name + " wake up! Are you alright?")
            print("Huh?  Oh... it's just an interesting dream.")
            print("Whew, what a weird nightmare... you sigh.")
            print("This is one of the 8 endings, thanks for playing!")



    #============================================ If you explore the beach  =========================================    
elif (explore == 2):
    print("You decide to go for a walk near the water.")
    print("You don't find anything out of the ordinary, it's just a beach.")
    print("Do you want to relax on the beach?")
    print("1: Yes\n2: No\n")
    relax = int(input("Relax?"))
    while (relax < 1 or relax > 2):
        print("Enter 1 or 2")
        relax = int(input("Relax?"))
    if (relax == 1):
        print("You laid down on the sand.")
        print("You fall asleep.")
        print("Thump, thump, .... THUD\n")
        print("You open your eyes,everything is shaking.")
        print("It's, it's... an earthquake!")
        print("The raggedy hut collapses, any clues that might've been there are destroyed.\n")
        print("You go examine the remains of the hut.")
        print("Within the sand you find a flare gun!")
        print("You use it, and a ship picks you up.")
        print("You've escaped!")
        print("This is one of the 8 endings, thanks for playing!")
    if (relax == 2):
        print("With nothing else to do you go back into the hut.\n")
        print("Upon further examination you see a small vial full of some liquid.")
        print("The liquid is clear and odorless.")
        print("I should:\n1: Drink it.\n2: Just put in my pocket.\n")
        drink =int(input("Drink it or save for it later?"))
        if(drink == 1):
            print("You're not sure what you just drank, but one thing's for sure,")
            print("you are probably going to faint.")
            print("Everything goes black...\n")
            print("Hey! Wake up!")
            print("You wake up to see two lifeguards standing over you.")
            print("You passed out from dehydration, and everything was just a hallucination.")
            print("This is one of the 8 endings, thanks for playing!")
        if(drink == 2):
            print("You put it in your pocket.")
            print("You are no closer to escaping than before.\n")
            print("All of a sudden you catch a glimpse of ship passing by!")
            print("Maybe if they hear you you can catch a ride off this island!\n")
            print("Do you want to call out to them?")
            print("1: Yes\n2: No\n")
            scream = int(input("Try asking for help?"))
            if(scream == 1):
                if(help == 1):
                    print("Your screams were successful!")
                    print("You are saved!")
                    print("This is one of the 8 endings, thanks for playing!")
                if(help == 2):
                    print("Whoever was on the ship didn't seem to hear you.")
                    print("It seems like there's nothing you can do.")
                    print("Game Over.")
                    print("This is one of the 8 endings, thanks for playing!")
            if(scream == 2):
                print("Nobody comes to your rescue.")
                print("You are trapped, and without any food or water you slowly perish.")
                print("Game Over.")
                print("This is one of the 8 endings, thanks for playing!")
            while (scream < 1 or scream > 2):
                print("Enter 1or 2.\n")
                scream =int(input("Try asking for help?"))
        while (drink < 1 or drink > 2):
            print("Please enter either 1 or 2.\n")
            drink =int(input("Drink it or save for it later?"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM