簡體   English   中英

函數未在“ if”語句內運行Python 3

[英]Function not Running inside an “if” statement Python 3

我試圖用Python編寫逃生室游戲。
當我使用pycharm運行代碼時,該過程以“以退出代碼0結束的過程結束”結束。

函數的定義有問題嗎?
這是我的代碼(有點冗長):

choice = None

def main_choice():

    print("1. Examine door")

    print("2. Examine painting")

    print("3. Examine desk")

    print("4. Examine bookshelf")

    choice == input("Make your choice: ")


def door():

    print("1. Try to open door")

    print("2. Take a closer look")

    print("3. Go back to where you were.")

    door_choice = input("What now? ")

    if door_choice == "1":

        print("The door is too heavy to open with your bare hands.")

        door()

    if door_choice == "2":

        print("There is a red light above the door, but it seems to have no purpose.")

        print("You notice a 9 key keypad next to the door. It looks like it will accept a 3 digit code.")

        keypad_code = input("Would you like to enter a code?").lower

        if keypad_code == " yes":

             guess = input("ENTER CODE: ")

             if guess == complete_key:

             print("The light turns green and you hear the sound of a mechanical lock unlocking. The door opens.")

             print("YOU WIN!!!")

        if guess != complete_key:

             print("Wrong code.")

             main_choice()

    if door_choice == "3":

        main_choice()

    else:

        print("You didn't answer")


main_choice()

if choice == "1":

    print("You walk to the door")

    door()

我認為這可能是最后的“如果”陳述,但我並不肯定。

您需要在main_choice函數main_choice choice == input("Make your choice: ")更改為choice = input("Make your choice: ")

def main_choice():

    print("1. Examine door")

    print("2. Examine painting")

    print("3. Examine desk")

    print("4. Examine bookshelf")

    choice = input("Make your choice: ")

否則, choice變量將仍然為None並且if choice == "1":始終為False。

您應該寫:

choice = input("Make you choice: ")

而不是:

choice == input("Make you choice: ")

雙等號返回布爾值而不是更改值。

暫無
暫無

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

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