Почему мой код не следует инструкции if / else и вместо этого переходит в другое место? - PullRequest
0 голосов
/ 02 марта 2019

Я должен сделать текстовую приключенческую игру для школьного проекта.Я застрял в точке, где пользователя просят «Введите 1, чтобы спрятаться, введите 2, чтобы противостоять Джейкобу».

Вместо того, чтобы код делал то, что я от него ожидаю, если пользователь вводит '1', он печатает 'Джейкоб достает пистолет и стреляет в тебя ...'

что я хочудля этого нужно напечатать строку «Ты прячешься за дровами ...»

Я знаю, что мой код довольно грязный, мне очень жаль.

Кто-нибудь знает, что я здесь не так сделал?

enter = input("Do you enter the cabin? >")
if enter == 'Yes' or enter == 'yes' or enter == 'Y' or enter == 'y':
    print("you decide to enter the cabin. There's someone in there. You recognise their face from the ID card you found earlier. It's Jacob Adams.")
    print("JACOB:", name_f+',',"what are you doing here. You're alive?")
    response = input("Enter 1 for 'Yes, I'm alive. I'm guessing that's no thanks to you!' Enter 2 for 'What are you talking about? Who are you? What the hell is going on?'")
    if response == '1':
        print("JACOB: This is nothing personal,", name_f)
    elif response == '2':
        print("JACOB: Don't worry,", name_f+',',"It'll all make sense soon enough")
    print("Jacob pulls out a gun and shoots you in the head before you have a chance to respond.")
    print("GAME OVER")
if enter == 'No' or enter == 'no' or enter == 'N' or enter == 'n':
    print("You decide to stay on the safe side. You creep around the back of the cabin and peer in through the window. You can't believe your eyes")
    print("There's someone in there. You recognise their face from the ID card you found earlier. It's Jacob Adams")
    print("He seems to be on the phone with someone")
    spy = input("Do you a)listen to the conversation or b)walk away before he sees you")
    if spy == 'a' or spy == 'A' or spy == 'listen' or spy == 'Listen' or spy == 'listen to the conversation' or spy == 'Listen to the conversation':
        print("JACOB: Yes I took care of it. I'm pretty sure", pronoun+"'s", "dead.")
        print('JACOB: I left', pronoun_2, 'in the middle of nowhere. Even if', pronoun, 'somehow survived,', pronoun, "won't have lasted five minutes where I left", pronoun_2)
        print('JACOB:',pronoun+"'s", 'probably under a foot of snow by now')
        print('JACOB:...')
        print('JACOB:...')
        print('JACOB:...')
        print('JACOB: Damnit!', pronoun.title(), "still has my diary. We can't have someone find that with the body. I should go back and get it")
        print("Jacob gets up and heads towards the door.")
        hide = input("Enter 1 to hide. Enter 2 to confront Jacob")
        if hide == 1:
            print("you hide behind some firewood. He doesn't see you.")
            print("Now's your chance:")
            diary = input("Do you a)read Jacob's diary or b)enter the cabin")
            if diary == "read Jacob's diary" or diary == "Read Jacob's diary" or diary == "read jacob's diary" or diary == "Read jacob's diary" or diary == 'a' or diary == 'A' or diary == 'Read diary' or diary == 'read diary' or diary == 'Diary' or diary == 'diary':
                code = input("Enter a four digit code")
                if code == '1302':
                    print("You unlocked the diary")
                    print("As you read the diary, your memories come back to you. Jacob, your business partner, and", spouse_name, "had been conspiring to kill you for the last two years. Once you died", spouse_name, "was supposed to receive your three million dollar inheritance.")
                    print(spouse_name, "had promised to split the money with Jacob in return for murdering you and disposing of your body. Luckily, a few months ago you found Jacob's bag lying around. You were suspicious that he was trying to cheat you out of your business, and decided to snoop")
                    print("You found his diary, and instantly knew that the code would be 1302, the date that you and Jacob founded your startup. When you learnt of", spouse_name, "Jacob's plot, you grabbed the bag and went into hiding. Unfortunately Jacob eventually found you, knocked you out cold and left you for dead in the middle of nowhere.")
                    print("You learn that the cabin belong's to Jacob's late grandfather. Jacob keeps a handgun in the sidetable drawer and a knife in the sofa cushion.")
                    print("You need to call the police!")
                    final = input("Enter 1 to enter the Cabin and look for supplies or Enter 2 to shout for help. >")
                    if final == '1':
                        print("You enter the cabin. You see a fireplace, some food and a telephone")
                        choice = input ("What do you do now? >")
                        print("Jacob had forgotten something back at the cabin. He comes back in to retreive it. You've been spotted.")
                        print("JACOB:", name_f+',',"what are you doing here. You're alive?")
                        response = input("Enter 1 for 'Yes, I'm alive. I'm guessing that's no thanks to you!' Enter 2 for 'What are you talking about? Who are you? What the hell is going on?'")
                        if response == '1':
                            print("JACOB: This is nothing personal,", name_f)
                        elif response == '2':
                            print("JACOB: Don't worry,", name_f+',',"It'll all make sense soon enough")
                        print("You know that Jacob is dangerous. You recall the gun and the knife that Jacob keeps in his cabin")
                        weapon = input("Enter 1 to go for the gun or Enter 2 to go for the knife >")
                        if weapon == '1':
                            print("you run to the sidetable to look for the handgun but when you open the drawer it's empty")
                            print("Jacob pulls out a gun and shoots you in the head")
                            print("GAME OVER")
                        elif weapon == '2':
                            print("You run to the sofa cushion and pull out a knife")
                            print("As you threaten Jacob with your knife, he pulls out a handgun and holds it up to your head")
                            print("you notice a bright ray of light shining in through the window and use your knife to reflect it back into Jacob's eyes.")
                            print("While he is still stunned by the light, you take the opportunity to grab his gun and shoot him in the leg")
                            print("You run to the telephone and call the police")
                            print("CONGRATULATIONS!")
                    elif final == '2':
                        print("Oh dear, it looks like Jacob heard you...")
                        print("JACOB:", name_f+',',"what are you doing here. You're alive?")
                        response = input("Enter 1 for 'Yes, I'm alive. I'm guessing that's no thanks to you!' Enter 2 for 'What are you talking about? Who are you? What the hell is going on?'")
                        if response == '1':
                            print("JACOB: This is nothing personal,", name_f)
                        elif response == '2':
                            print("JACOB: Don't worry,", name_f+',',"It'll all make sense soon enough")
                        print("Jacob pulls out a gun and shoots you in the head before you have a chance to respond.")
                        print("GAME OVER")

                else:
                    print("you only had one chance to enter the code. The diary is locked forever. You walk into the cabin to look for clues instead.")
                    print("Jacob forgot something in the cabin and comes back in to retreive it. You've been spotted.")
                    print("JACOB:", name_f+',',"what are you doing here. You're alive?")
                    response = input("Enter 1 for 'Yes, I'm alive. I'm guessing that's no thanks to you!' Enter 2 for 'What are you talking about? Who are you? What the hell is going on?'")
                    if response == '1':
                        print("JACOB: This is nothing personal,", name_f)
                    elif response == '2':
                        print("JACOB: Don't worry,", name_f+',',"It'll all make sense soon enough")
                    print("Jacob pulls out a gun and shoots you in the head before you have a chance to respond.")
                    print("GAME OVER")

            elif diary == 'enter the cabin' or diary == 'b' or diary == 'B' or diary == 'Enter the cabin' or diary == 'Enter cabin' or diary == 'enter cabin':
                print("Jacob forgot something in the cabin and comes back in to retreive it. You've been spotted.")
                print("JACOB:", name_f+',',"what are you doing here. You're alive?")
                response = input("Enter 1 for 'Yes, I'm alive. I'm guessing that's no thanks to you!' Enter 2 for 'What are you talking about? Who are you? What the hell is going on?'")
                if response == '1':
                    print("JACOB: This is nothing personal,", name_f)
                elif response == '2':
                    print("JACOB: Don't worry,", name_f+',',"It'll all make sense soon enough")
                print("Jacob pulls out a gun and shoots you in the head before you have a chance to respond.")
                print("GAME OVER")
        elif hide == 2:
            print("JACOB:", name_f+',',"what are you doing here. You're alive?")
            response = input("Enter 1 for 'Yes, I'm alive. I'm guessing that's no thanks to you!' Enter 2 for 'What are you talking about? Who are you? What the hell is going on?'")
            if response == '1':
                print("JACOB: This is nothing personal,", name_f)
            elif response == '2':
                print("JACOB: Don't worry,", name_f+',',"It'll all make sense soon enough")
        print("Jacob pulls out a gun and shoots you in the head before you have a chance to respond.")
        print("GAME OVER")

1 Ответ

0 голосов
/ 02 марта 2019

input() дает вам строку - вы сравниваете ее с целым числом:

hide = input("Enter 1 to hide. Enter 2 to confront Jacob")
if hide == 1:

Строка никогда не равна целому числу, замените ее на:

hide = input("Enter 1 to hide. Enter 2 to confront Jacob")
if hide == "1":

или преобразовать в int (произойдет сбой, если будет введен текст не-int):

hide = int( input("Enter 1 to hide. Enter 2 to confront Jacob"))
if hide == 1:

Вы отправили гораздо больше кода, чем нужно - этого было бы достаточно для публикации:

hide = input("Enter 1 to hide. Enter 2 to confront Jacob")
if hide == 1:
    # do smth 
    pass
elif hide == 2:
    # do smth other
    pass

Чтобы избежать необходимости сравнивать столько вариантов, вы можете lower() ввести и сравнить со списком вещей:

spy = input("Do you a)listen to the conversation or b)walk away before he sees you")
if spy == 'a' or spy == 'A' or spy == 'listen' or spy == 'Listen' or spy == 'listen to the conversation' or spy == 'Listen to the conversation':

isкороче, если вы делаете:

spy = input("Do you a)listen to the conversation or b)walk away before he sees you").lower()

# you could use a set - but python will inline this as frozenset anyhow
if spy in ('a' ,'listen','listen to the conversation'):

Возможно, вы захотите прочитать Как отлаживать небольшие программы , чтобы получить некоторые советы по отладке.

...