2017-10-19 112 views
-1

我是Python的新手,我試圖創建一種非常特定的循環。我讀過的東西確實幫助了我。也許因爲這不是我的意思,或者因爲我不明白。我會試着問,希望有人能理解我。我基本上只是試圖重播上面的四條線。在Python中創建if/else循環

def defoption(): 
    option = ("Give an input") #<- Trying to replay this line 
    option_input = input("Input: ") #<- and then this 
    if (option_input == 'a'): 
     print("Option a works") 
    else: 
     return option and print("Option unavailable") 
defoption() 
+0

你的問題實在是不清楚......你想 「重演」 什麼?你想達到什麼目的? – Sayse

+0

預期產量是多少? –

+1

也許你需要一個循環,雖然選項不等於某個東西,但你重複該功能? – rmjoia

回答

-1

這個什麼:

def defoption(): 
    print("Give an input") #<- Trying to replay this line 
    option_input = input("Input: ") #<- and then this 
    if option_input == 'a': 
     print("Option a works") 
    else: 
     print("Option unavailable") 
     return defoption() 

defoption() 
+0

Woah,我一直在爲此工作3個小時,我甚至沒有嘗試過...... *最大的facepalm * 非常感謝您指出 – mwaning

+0

很高興聽到它的工作!想知道爲什麼有人會倒下我的答案,雖然... – mrCarnivore

+0

這是一個壞主意,以回答重複,尤其是[回答鏈接問題](https://stackoverflow.com/a/23294659/3650362)顯然更好,甚至*解釋了爲什麼你的代碼只有答案是有缺陷的* – trentcl

0
def defoption(): 
    print("Give an input") 
    option_input = input("Input: ") 
    return option_input 

opt = defoption() 
while(opt not in ['a']): 
    print("Option unavailable") 
    opt = defoption() 
print("Option {} works!".format(opt)) 
+0

我提高了你的答案,因爲我不贊同trentcl的推理。 – mrCarnivore

+0

謝謝corn3lius。這不是我正在尋找的東西,但我確信一段時間的循環可能會很快派上用場。 – mwaning