在Python中创建if / else循环

时间:2017-10-19 13:44:16

标签: python loops

我是Python的新手,我试图创建一种非常特殊的循环。我读过的任何内容都没有帮助我。也许是因为它不是我的意思,或者可能是因为我不明白。我会尝试询问并希望有人理解我。我基本上只是试图从顶部重播四条线。

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

2 个答案:

答案 0 :(得分: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))

答案 1 :(得分:-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()