2017-10-19 35 views
1

我還在學習Python,但是我的朋友在Python中編程之前說過這應該可以正常工作,但它不會?Python遊戲;爲什麼我不能重新調用我的輸入和if/else函數?

在此之前的所有代碼是這個基本的「逃出房間」的遊戲我正在開始的故事。代碼直到這裏才起作用(描述遊戲的基本打印功能)。

我給玩家,他們在一個房間裏是該方案,他們可以做兩件事情之一:

def intro_room_input(): 
    intro_action = input("What would you like to do? (Please enter either: 1 or 2) ") 
    return intro_action; 

這兩個功能是當他們選擇1或2,接下來,如果/ ELIF功能如果他們選擇1運行這些功能 :

def intro_room_result1(): 

print(
    """ 
    (Story stuff for the result of option 1. Not important to the code) 
    """) 

    return; 

此功能將發揮出來,如果他們選擇2

def intro_room_result2(): 

    print(
    """ 
    (Story stuff for the result of option 2. Not important to the code) 

    """) 

    return; 

這將用於接收玩家的輸入並從那裏繼續故事。

def intro_action_if(string): 

    if string == "1":  
     intro_room_result1() 
    elif string == "2": 
     intro_room_result2() 
    else: 
     print("I'm sorry, that wasn't one of the options that was available..."+'\n'+ 
     "For this action, the options must be either '1' or '2'"+'\n'+ 
     "Let me ask again...") 
     intro_room_input() 
     intro_action_if(string) 
    return; 
去年intro_room_input運行正常

,它重新運行先前的輸入,但是當你真正進入1或2,它並沒有對他們什麼。它不想重新運行if/elif/else函數來給出結果。

最後我有一個主運行一切:

def main(): 
    string = intro_room_input() 
    intro_action_if(string) 
    return; 


main() 

請幫幫忙,我不知道什麼是錯,此代碼!?

+0

這是我所看到的:在else語句你是不是分配的'intro_action_if'到'string',因此呼叫將再次做同樣的事情的結果。 –

回答

1

問題出在您的intro_action_if()。當您調用函數以再次獲取值時,您忘記更改string值。

#intro_room_input()   #wrong 

string = intro_room_input() #right 
intro_action_if(string) 

正如你可以看到,即使在你的代碼你問用戶inputreturned它,你忘了重新分配string與返回的值。因此,它保持您之前給出的相同輸入並將該舊值傳遞給intro_action_if()

+0

非常感謝! :)重新分配它在我的功能工作就像我想要的。 –

相關問題