简体   繁体   English

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

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

This is a chatbot, that includes a choose your own adventure game, however if you select the 2nd option for the first choice (explore the beach) it doesn't execute anything after that in the game.这是一个聊天机器人,其中包括一个选择您自己的冒险游戏,但是如果您选择 select 作为第一选择的第二个选项(探索海滩),它不会在游戏中执行任何操作。

When I select the first option, everything works fine, so I don't understand why it's not showing.当我 select 第一个选项时,一切正常,所以我不明白为什么它没有显示。 The game itself works fine if I run it separately from the chatbot, and I've checked the indenting already.如果我将游戏与聊天机器人分开运行,游戏本身就可以正常运行,并且我已经检查了缩进。 Please help.请帮忙。

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

The problem with your code is you have put the statement if explore == 2 inside the if explore == 1 .您的代码的问题是您已将语句if explore == 2放入if explore == 1中。 Python gives a lot of importance to indentation. Python 非常重视缩进。 All you need to do is indent the second if statement with the first one.您需要做的就是将第二个if statement与第一个 if 语句缩进。 In your code.在你的代码中。

Sample:样本:

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

This is the sample of your statement.这是你的陈述样本。 Here, x=2.这里,x=2。 First it checks if x==1.首先它检查 x==1。 If no then goes to other statement.如果否,则转到其他语句。 But you have defined if x==2 inside the first one.但是您已经在第一个内部定义了if x==2 So it will be executed only if the first statement is True.所以只有当第一条语句为 True 时才会执行。

So the correct expression is:所以正确的表达方式是:

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

This is the case in your code.您的代码就是这种情况。

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.

相关问题 为什么执行代码后在 pipenv 环境中显示语法错误 - why Syntax errors shown in pipenv environment after executing the code 将字符串作为python代码执行并存储输出+错误 - Executing a string as python code and storing output + errors 在PyQT中显示QDialog之后执行代码的惯用方式是什么? - What is an idiomatic way of executing code AFTER the QDialog is shown in PyQT? 尽管只需要执行一部分代码,但执行所有行的代码 - Code executing all lines despite only required to excute a part of code 为什么我的python代码不执行,而是给我一个“无”错误,而只执行其中的一部分? - Why is my python code not executing, but instead giving me a “None” error and only executing part of it? 我的海龟模块代码正在执行第一部分而不是第二部分。 两个部分都可以自己正常工作 - My turtle module code is executing the first part but not the second part. Both parts are working fine on their own 函数在运行时甚至没有调用代码,你能帮我更新它吗,终端中没有显示错误 - The Function is running without even calling in the code , can you update it for me , no errors shown in terminal 在3.x程序中有条件地将python代码的某些部分作为2.x执行 - Conditionally executing some part of python code as 2.x in a 3.x program 并非始终显示lmfit参数上的错误 - The errors on lmfit parameters are not always shown Visual Studio Code 在执行 if 语句时不断给我缩进或语法错误? - Visual Studio Code keeps giving me indent or syntax errors when executing if statements?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM