首页 > 解决方案 > 代码没有读取 Elif 或 Else 语句

问题描述

我正在创建一个“选择你自己的冒险”,其中一些选项有效,但它们并非都完全正常。因此,当我选择属于代码的 elif 部分的特定答案时,就像程序甚至不读取它并且只使用 If 语句一样。

我将整个项目设置在这里 JIC 它是需要的,但我需要帮助。我试图重新格式化和一切。我错过了什么?

    import time

#declare health
health = 100

#declare game constant
game = True


#declare wins and loses
wins = 0
loses = 0

# Defining for bolding of lines later
class color:
    PURPLE = '\033[95m'
    CYAN = '\033[96m'
    DARKCYAN = '\033[36m'
    BLUE = '\033[94m'
    GREEN = '\033[92m'
    YELLOW = '\033[93m'
    RED = '\033[91m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
    END = '\033[0m'


#Winning line
def win():
    print( "You've survived!" )

#loosing line but pirate-y
def losepirate():
    print( "Arrghhh... You've lost matey. Walk the plank.")

#losing line but regular
def lose():
    print( "You've lost this time. Never fear though, I'm sure your next round will be better." )

#the song that will play before your pirate demise
def piratelife():
    print( "Yo ho, yo ho, a pirate's life for me." ) 
    time.sleep(1)
    print( "We pillage plunder, we rifle and loot." )
    time.sleep(1)
    print( "Drink up me 'earties, yo ho." )
    time.sleep(1)
    print( "We kidnap and ravage and don't give a hoot" )
    time.sleep(1)
    print( "Drink up me 'earties, yo ho." )
    time.sleep(1)
    print( "Yo ho, yo ho, a pirates life for me." )
    time.sleep(1)
    print( "We extort and pilfer, we filch and sack." )
    time.sleep(1)
    print( "Drink up me 'earties, yo ho." )
    time.sleep(1)
    print( "Maraud and embezzle and even highjack." )
    time.sleep(1)
    print( "Drink up me 'earties, yo ho." )
    time.sleep(2)
    print( "Yo ho, yo ho, a pirate's life for me." )
    time.sleep(1)
    print( "We kindle and char and in flame and ignite" )
    time.sleep(1)
    print( "Drink up me 'earties, yo ho." )
    time.sleep(1)
    print( "We burn up the city, we're really a fright." )
    time.sleep(1)
    print( "Drink up me 'earties, yo ho." )
    time.sleep(1)
    print( "We're rascals and scooundrels, we're villains and knaves." )
    time.sleep(1)
    print( "Drink up me 'earties, yo ho." )
    time.sleep(1)
    print( "We're devils and black sheep, we're really bad eggs" )
    time.sleep(1)
    print( "Drink up me 'earties, yo ho. " )
    time.sleep(1)
    print( "We're beggars and blighters and ne'er do well cads." )
    time.sleep(1)
    print( "Drink up me 'earties, yo ho." )
    time.sleep(1)
    print( "Aye, but we're loved by our mommies and dads..." )
    time.sleep(1)
    print( "Drink up me 'earties, yo ho." )
    time.sleep(3)
    print( "Yo ho, yo ho, a pirate's life for... me." )
    time.sleep(2)


    #The song ive included because a line i wrote made me think of this and
    # it then HAD to be included
def moana():
    print( "I've been staring at the edge of the water" )
    print( "'Long as I can remember, never really knowing why...")
    time.sleep(3)
    print(color.BOLD + "Umm as the narrator I can tell you that it hasn't been that long - " + color.END)
    print( "I wish I could be the perfect daughter")
    print( "But I come back to the water, no matter how hard I try.")
    time.sleep(2)
    print(color.BOLD + 'Okay, Imma let you have your moment.' + color.END)
    print( "Every turn I take, every trail I track, every path I make, EVERY road leads back..." )
    print( "To the place I know where I cannot go, WHERE I LONG TO BE!")
    print( "See the line where the sky meets the sea? IT CALLS ME!")
    print( "And no one knows, how far it goes.")
    print( "If the wind in my sail on the sea stays behind me....")
    print( "One day I'll know... how far I'll go.")
    time.sleep(5)
    print(color.BOLD + "So, you're gonna sail off the island and save yourself?" + color.END)
    print( "YES!!!! THE SEA CALLS ME!!!")
    time.sleep(3)


def cont(yn):
    if yn == 'y':
        return True
    elif yn == 'n':
        return False


# Main Choose Adventure Run

while game == True:
    print( "You've been stranded on an island. You only have yourself." )
    print( "There is nothing else to help you." )
    print( "Which direction will you walk?" )
    choice_direction = input( "Left or Right? " )
    if choice_direction == 'Left' or 'left':
        direction = 'left'
        health -= 10
        print( "Okay. You find a jelly fish walking to the left and a deer galloping into the forest." )
        print( "Which do you follow?" )
        animal_choice = input( "Follow jellyfish? ")
        if animal_choice == 'yes' or 'Yes':
            health +=5
            print( "You've found water!" )
            print( "Do you drink some?" )
            water_choice = input( "Yes or No ")
        else:
            print( "Well then, don't let me stop you. But I'm not going to go with you on that.")
            print( "You go on and I'll check back on you in a min." )
            time.sleep(10)
            print( "You have found a stick")
            print( "All of a sudden you start to do magic with the stick" )
            print( "Suddenly there is foliage, no dangerous creatures and plenty of food.")
            print( "You look at the narrator. 'I've got this under control." )
            print( "'I guess you do' the narrator sighs. Then leaves.")
            print( "You've won the game.... oddly.")
            health = 500
            win += 1
            if water_choice == 'yes' or 'Yes':
                health += 5
                print( "Good job! You've had water now! But you're hungry..." )
                print( "Where will you go?")
                food_choice = input( "Try and find the deer or fish in the sea. ")
            elif water_choice == 'no' or 'No':
                print( "Well, that was dumb.. you were suffering from dehydration already." )
                print( "The water was safe to drink too.")
                print( "Oh well..." )
                health -= 75
                print( "You see a ship to your left, do you go to the ship?" )
            else:
                print("Choose a valid input please!")
                ship_choice2 = input( "Yes or No? ")
                if ship_choice2 == 'yes' or 'Yes':
                    print( "You start walking towards the ship..." )
                    lose()
                    health -= 35
                    print( "You died of dehydration.")
                    loses += 1
                elif ship_choice2 == 'No' or 'no':
                    print( "You sit down on the beach, contemplating your life.")
                    print( "You fall asleep with the sun blaring on you")
                    print( "You wake up and realize it was all a bad dream." )
                    print( "You are terrified of the beach now. ")
                    print( "Uhhhh.... game over. I guess you won?" )
                    wins += 1
                else:
                    print( "You know what... you lose just cause. I'm giving you to the pirates.")
                    losepirate()
                    loses += 1
                    if food_choice == 'fish' or 'fish in the sea' or 'Fish':
                        health += 5 
                        print( "You go out to the sea, and you see small splashes of fish to your right ")
                        print( "You also see a ship to your left." )
                        hunting = input( "Left or Right?" )
                        if hunting == 'Left' or 'left':
                            print( "You hear something... strange... a song...")
                            piratelife()
                            time.sleep(2)
                            print( "That was a pirate's ship you stumbled upon and tried to steal from..." )
                            time.sleep(2)
                            print( "They made you walk the plank... :( ")
                            health = 0
                            losepirate()
                            loses += 1
                        elif hunting == 'Right' or 'right':
                            print( "You follow the way the deer went." )
                            print( "You find a red berry and a blue berry while looking for a meal." )
                            berry_choice = input( "You're hungry now and don't wanna wait for the deer. Do you eat the red berry or the blue one?" )
                            if berry_choice == 'red berry' or 'red' or 'Red Berry':
                                health += 5
                                print( "You're hunger has been satisfied... but you are tired.")
                                print( "Where will you choose your shelter for the night?")
                                shelter_choice = input( "Where you are at with trees and bush or on the beach?" )
                                if shelter_choice == 'beach' or 'Beach':
                                    print( "You go make a shelter on the beach" )
                                    print( "You make a fire and keep warm" )
                                    print( "As you drift off, the fire is blazing..." )
                                    print( "This is the fire that gets the Coast Guard to notice you." )
                                    print( "They wake you up, take you with them and you are saved.")
                                    win()
                                    wins += 1
                                elif shelter_choice == 'trees and bush' or 'trees' or 'Trees':
                                    print( "You set up your camp right there." )
                                    print( "Tired after making a fire... you fall asleep." )
                                    print( "The fire attracts the Urban Legend of The Great Centaur." )
                                    print( "The Centaur kidnaps you and takes you back to the Centaurian cave." )
                                    print( "You had no idea, but human is a delicacy for Centaurs... ")
                                    print( "You are eaten.")
                                    health = 0
                                    lose()
                                    loses +=1
                                else:
                                    print( "Pick a valid input!" )
                            elif berry_choice == 'blue berry' or 'blue':
                                print( "Welp... that was the poisonious one...")
                                print( "You'll start to feel tingling in your fingers and lips..." )
                                print( "As your whole body swells..." )
                                print( "The only thing that will cure it is a time turner..." )
                                print( "If you know the password... then you will be able to turn back time.")
                                time_turner = input("Do you know it? ")
                                if time_turner == 'yes' or 'Yes':
                                    print( "All right then. One shot at the password.")
                                    fate = input( "Type it out: ")
                                    if fate == 'Coffee' or 'coffee':
                                        print( "My God... You did it." )
                                        print( "You know what... you win. I'm shocked you got that..." )
                                        win()
                                        wins += 1
                                    else:
                                        lose()
                                        loses += 1
                                else:
                                    lose()
                                    loses += 1
                            else:
                                print( "Pick a valid input!" )
                        else:
                            print("Pick a valid choice!")
    elif choice_direction == 'right' or 'Right':
        direction = 'right'
        health += 100
        print( "Okay, we're going to the right. Wanna know what's over there?" )
        print( "Mountains. Mountains are over there... Are you gonna climb them?")
        mountain_choice = input( "Yes or no?" )
        if mountain_choice == 'yes' or 'Yes':
            print( "You climb the mountains and get to the top. From here you begin to ponder what life is.")
            print( "You see the sea meet the sky and suddenly you start singing..." )
            moana()
            print( "And then you did. And you saved Te Fiti. And lived out a life like Moana.")
            print( "You even met Maui the demi God.")
            time.sleep(2)
            print( "I guess I could say, You're Welcome.")
            health += 500
            win += 1
        else:
            print( "You get bit by a mountain snake while you were at the base of the mountain because you didn't climb the mountain.")
            time.sleep(1)
            print("Yikes")
            lose()
            loses += 1
    else:
        print( "Choose a valid input cause thats the only choice you got right now." )

            

print('You have beaten the game', (wins), 'times')
print('The game has beaten you', (loses), 'times.' )
time.sleep(2)

decision = input( 'Do you want to try again? y/n')
game = cont(decision)

标签: pythonif-statement

解决方案


使用不当的例子if。更改choice_direction == 'Left' or 'left'为:

choice_direction.lower() == 'left'

总的来说,代码应该被重组为事件驱动......

def handle_Choice_direction():
     choice_direction = input( "Left or Right? " )
     # do function Choice Direction
     handle_mountain_Choice()

def handle_mountain_Choice():
     mountain_choice = input( "Yes or No? " )
     # do function mountain choice

推荐阅读