如何在我的 python 脚本中重新启动 main() 函数?无论我添加什么输入,程序都会重新启动
How do I restart the main() function in my python script? The program restarts no matter what input I add
如何确保在输入“Y”“y”“yes”时程序重启?然后如果输入任何其他内容则程序结束?此时如果输入任何内容(包括什么都不输入)然后我按回车键,程序将重新启动。
我为此编写的代码如下,它全部包含在 main() 函数中:
def main():
# Main program code here
# Ask the player whether they want to retry
if display == wordChosen:
tryAgain = str(input("Try again: (Y = yes / N = no)"))
tryAgain = tryAgain.lower()
if tryAgain == "Y" or "y" or "yes":
print(tryAgain) # for testing just to see whether its being picked up
main()
else:
exit
main()
您可以将 if 语句更改为:if tryAgain == "y" or tryAgain == "yes":
因为您已经将 tryAgain 输入转换为小写,所以无需检查其“Y”是否为大写,因为它永远不会被大写。 if tryAgain == "Y" or "y" or "yes":
行检查 tryAgain 是否为“Y”,然后检查“y”是否为真或“yes”是否为真,而不是 tryAgain 是否为“y”或“yes”,据我所知不- 空字符串始终为真。
如何确保在输入“Y”“y”“yes”时程序重启?然后如果输入任何其他内容则程序结束?此时如果输入任何内容(包括什么都不输入)然后我按回车键,程序将重新启动。
我为此编写的代码如下,它全部包含在 main() 函数中:
def main():
# Main program code here
# Ask the player whether they want to retry
if display == wordChosen:
tryAgain = str(input("Try again: (Y = yes / N = no)"))
tryAgain = tryAgain.lower()
if tryAgain == "Y" or "y" or "yes":
print(tryAgain) # for testing just to see whether its being picked up
main()
else:
exit
main()
您可以将 if 语句更改为:if tryAgain == "y" or tryAgain == "yes":
因为您已经将 tryAgain 输入转换为小写,所以无需检查其“Y”是否为大写,因为它永远不会被大写。 if tryAgain == "Y" or "y" or "yes":
行检查 tryAgain 是否为“Y”,然后检查“y”是否为真或“yes”是否为真,而不是 tryAgain 是否为“y”或“yes”,据我所知不- 空字符串始终为真。