繁体   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