2016-11-06 56 views
0

我試圖在Python中編寫一個逃生房間遊戲。
當我用pycharm運行代碼時,進程以「進程完成退出代碼0」結束。函數不在「if」語句中運行Python 3

定義函數有什麼不對嗎?
這裏是我的代碼(它是一個有點冗長):

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

我想,這可能是最後的「如果」的聲明,但我還不能肯定。

+0

基本上你想不停地運行程序嗎? –

+0

調用此方法的一種快速方法是在調用'main_choice()'後調試打印,以確保您已將「choice」分配給某個東西。 – scrappedcola

回答

1

您需要更改choice == input("Make your choice: ")choice = input("Make your choice: ")main_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變量仍然是Noneif choice == "1":將永遠是假的。

1

你應該寫:

choice = input("Make you choice: ") 

而不是:

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

雙等號返回一個布爾值,而不是改變的值。