简体   繁体   English

函数未在“ if”语句内运行Python 3

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

I'm attempting to write an escape room game in Python. 我试图用Python编写逃生室游戏。
When I run the code with pycharm the process ends with "process finished with exit code 0." 当我使用pycharm运行代码时,该过程以“以退出代码0结束的过程结束”结束。

Is there something wrong in the defining of the functions? 函数的定义有问题吗?
Here is my code (its a bit lengthy): 这是我的代码(有点冗长):

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()

I think that it might be the last "if" statement, but I'm not positive. 我认为这可能是最后的“如果”陈述,但我并不肯定。

You need to change choice == input("Make your choice: ") to choice = input("Make your choice: ") in your main_choice function: 您需要在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: ")

Otherwise the choice variable will still be None and if choice == "1": will always be False. 否则, choice变量将仍然为None并且if choice == "1":始终为False。

You should write: 您应该写:

choice = input("Make you choice: ")

rather than: 而不是:

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

Double equals signs return a boolean rather than changing a value. 双等号返回布尔值而不是更改值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM