2017-10-19 111 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