【发布时间】:2019-07-01 22:58:26
【问题描述】:
我希望在我的 Python 程序中使用函数以使其更简洁、更高效。在我的函数中,我根据用户的选择返回 true 或 false。尽管在他们输入错误/无效响应的情况下,我想以不问更多问题的方式返回。
编辑:
更具描述性;我想重新创建这个:
def askquestion(question):
response = input(question, "Enter T or F")
if response == "T":
return True
elif response == "F":
return False
else:
return None
def askmultiple():
questionOne = askquestion("Do you fruits?")
if questionOne == None:
return # Exit the function, not asking more questions
questionTwo = askquestion("Do you Apples?")
if questionTwo == None:
return # Exit the function, not asking more questions
我想以后检查是否是None,然后直接返回。
【问题讨论】:
-
能否请您出示您目前拥有的代码,以便我们了解您在做什么
-
“我想退货”这没有意义。
return是语句的一部分,它不是对象,因此无法返回。你不妨问“我如何返回if” -
这听起来像是你想要Asking the user for input until they give a valid response 所要求的。我没有立即将其标记为重复的唯一原因是您的问题太模糊,无法确定它是重复的。