Python 是否仍有使用';返回';从';还有别的吗;?
我已经面临这个问题有一段时间了。我知道“return”命令不能在函数外工作,但是否有替代方法,或者是否需要定义新函数Python 是否仍有使用';返回';从';还有别的吗;?,python,Python,我已经面临这个问题有一段时间了。我知道“return”命令不能在函数外工作,但是否有替代方法,或者是否需要定义新函数 print("Are you", userage(), "years old?") userinput = input() if userinput == "yes": print("Ok, lets get on with the program") elif userinput ==&q
print("Are you", userage(), "years old?")
userinput = input()
if userinput == "yes":
print("Ok, lets get on with the program")
elif userinput =="no":
print("Oh, let's try that again then")
userage()
else:
print("please answer yes or no")
return userinput
顺便说一句,我为我犯的所有错误感到抱歉。我还是个初学者。在这里循环时,需要使用
:
while (True):
userinput = input("Are you xxx years old?\n")
if userinput == "yes":
print("Ok, lets get on with the program")
break
elif userinput == "no":
print("Oh, let's try that again then")
else:
print("please answer yes or no")
只有当有人键入yes
使自己熟悉循环时,循环才会中断,尤其是示例中的while循环。如果您想要返回,还需要函数。为什么不将您的输入和检查放入一个返回结果的程序中?@SirLad看看return
有什么作用?即使这是在函数中,它也会简单地退出函数,无论如何它都会这样做,因为它在末尾。你似乎认为return-userinput
意味着“回到我定义的userinput的地方”,但事实并非如此。@kindall-SirLad很清楚return
不是正确的选择,而是在试图弄清楚该怎么做。