簡體   English   中英

Python if-else 語句的問題

[英]Problems with a Python if-else statement

我正在為 IT 課程做作業。 非常基礎的 Python 項目。 我沒有這方面的經驗,但大部分時間都能掌握正在發生的事情。 該項目涉及做出涉及隨機整數選擇的選擇。 我可以獲得以 if-else 格式按照我想要的方式工作的選項,但 else 命令沒有按照我想要的方式響應。 選項都正確加載,但如果由於某種原因選擇為 1-3,程序將在打印“if”語句后打印“else”語句。 如果他們選擇選項 4 或選擇無效數字(else 語句的原因),則不會發生這種情況。

我在程序的前一部分中沒有這個問題。 我一定錯過了什么,但我不知道它是什么。 我應該重申,我是這方面的初學者,基本上是按照作業指示復制粘貼代碼,然后對其進行編輯以滿足我的需要。

interactions = ["Back Away Slowly","Point Towards the Chamber","Attack","Try To Communicate",]
import random
badchoice = random.randint(1,3)
loop = 1

while loop == 1:
    choice=menu(interactions, "How do you decide to interact?")
    print("")

    if choice == 1:
        print("You start to slowly back away from the man.")
        print("You worry you are giving off the wrong attitude.")
        print("")

        if choice == badchoice:
            loop=0
            print("The stranger was already weary of your presence.")
            print(interactions[choice-1], "was not the correct decision.")
            print("He calls for help. Villagers and guards immediately surround you.")
            print("You are thrown back into the chamber. Hitting your head, and getting knocked unconscious in the process.")
            print("You are back where you started and the items have been removed.")
            print("There is no escape.")
            print("Lamashtu's Curse can not be broken.")
            print("Game Over.")

        else:
            print("The stranger looks at you in confusion.")
            print("")

# Choices 2 and 3 go here. Same code. Seemed redundant to post all of it.    


    if choice == 4:
        loop=0
        print("The stranger is weary of your presence, and can not understand you.")
        print("You can see in his eyes that he wants to help,")
        print("and he escorts you back to his home for the time being.")
        print("He offers you some much needed food and water.")
        print("You are no closer to understanding what curse brought you here.")
        print("Perhaps tomorrow you will have more time to inspect your surroundings.")
        print("In time you may be able to communicate enough  with your host to explain your dilemma,")
        print("but for now you are trapped 6000 years in the past with no other options.")
        print("At least you've made it out alive thus far......")
        print("To be continued...")

    else:
        print("Please choose a number between 1 and 4.")

你的邏輯不對。 您最后的if語句將選擇4指向它的True分支,並將其他所有內容指向False分支。 Python 完全按照您的要求執行。 沒有對先前塊的邏輯引用。

你需要的邏輯是

if choice == 1:
    ...
elif choice == 2:
    ...
elif choice == 3:
    ...
elif choice == 4:
    ...
else:
    print("Please choose a number between 1 and 4.")

else僅適用於同一級別的最新if ,即數字 4 的if 。其他三個if語句都是獨立的。 因此,在檢查完每一個之后,它會檢查選擇是否為 4,如果不是,則運行 else。

您需要將所有這些都作為同一語句的一部分,您可以通過對第一個之后的每個 if 使用elif來做到這一點。 像這樣:

if choice == 1:
    ...
elif choice == 2:
    ...
elif choice == 3:
    ...
elif choice == 4:
    ...
else:
    ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM