簡體   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