在Python中创建if/else循环

在Python中创建if/else循环,python,loops,Python,Loops,我是Python新手,我正在尝试创建一种非常特殊的循环。我读过的书都没有真正帮助过我。也许是因为这不是我的意思,也许是因为我不明白。我会试着问,希望有人能理解我。我基本上只是想重播上面的四行 def defoption(): option = ("Give an input") #<- Trying to replay this line option_input = input("Input: ") #<- and then this if (option_inp

我是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()
def defoption():
选项=(“给出一个输入”)#这个呢:

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()
def defoption():

打印(“提供输入”)#你的问题真的不清楚。。。你想“重播”什么?您试图实现什么?预期的输出是什么?也许您需要一个循环,当option不等于某个值时,您可以重复该函数?您希望迭代多少次?
,而True:defoption()
祝您玩得开心。:德沃,我已经做了3个小时了,我甚至没有试过这个。。。最大的脸掌非常感谢你指出它很高兴听到它的工作!想知道为什么有人会否决我的答案…回答重复的答案是个坏主意,特别是当答案明显更好,甚至解释了为什么你的纯代码答案有缺陷时,因为你在我提供答案后发布了重复问题的链接,我不确定为什么我应该因为提供了一个明显有助于某人的答案而投反对票……对不起,在我发布我的答案之前,我没有找到这个帖子。只是后来出于某种原因,它才建议。。。尽管如此,我只是在写一些小脚本来了解语言是如何工作的,我非常感谢Carnivore先生现在提供了一个帮助我的答案。我对你的答案投了赞成票,因为我不同意trentcl的推理。谢谢corn3lius。这并不完全是我想要的,但我相信一个while循环很快就会派上用场。
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))