按 ENTER 退出程序 Python 不工作
Press ENTER to exit program in Python not working
试图让我的代码在按下回车按钮时退出,我 运行 进入 ValueError: invalid literal for int() with base 10: ''
,但它告诉我我的错误在 [=13] 行=].有人有想法吗?
simulations = input("How many times would you like to run the simulation? ")
# Invalid answer, system exit
if (not simulations) or int(simulations) <=0:
print("That is not a valid number of simulations.")
print("Shutting down.")
SystemExit
simulations_num = int(simulations)
#the rest of my code comes here
我也尝试过 if simulations == ''
但无济于事。
我注意到如果 simulations_num = int(simulations)
不涉及,代码工作正常,但代码的其余部分(或类似的东西)是必要的
您需要 raise SystemExit()
,或者只使用 sys.exit()
(做同样的事情)。
您需要提出错误,或者至少使用 sys.exit(0)
。
工作原理是这样的:
import sys
if not input(': ').lower() in ['yes','y','1']:
sys.exit(0)
应用于你的例子就是这个
simulations = input("How many times would you like to run the simulation? ")
import sys
# Invalid answer, system exit
if (not simulations) or int(simulations) <=0:
print("That is not a valid number of simulations.")
print("Shutting down.")
sys.exit(0)
simulations_num = int(simulations)
#the rest of my code comes here
raise RuntimeError()
也可以
解决您的代码:
首先,您需要在SystemExit的前面加上“raise”关键字,它才能正常工作。
其次,要建立一个必须按Enter键才能让程序退出的情况,可以使用input()。
更正版本:
simulations = input("How many times would you like to run the simulation? ")
# Invalid answer, system exit
if (not simulations) or int(simulations) <= 0:
print("That is not a valid number of simulations.")
print("Shutting down.")
input("Press Enter to exit...")
raise SystemExit("Exiting... Reason: invalid input!")
simulations_num = int(simulations)
# the rest of my code comes here
试图让我的代码在按下回车按钮时退出,我 运行 进入 ValueError: invalid literal for int() with base 10: ''
,但它告诉我我的错误在 [=13] 行=].有人有想法吗?
simulations = input("How many times would you like to run the simulation? ")
# Invalid answer, system exit
if (not simulations) or int(simulations) <=0:
print("That is not a valid number of simulations.")
print("Shutting down.")
SystemExit
simulations_num = int(simulations)
#the rest of my code comes here
我也尝试过 if simulations == ''
但无济于事。
我注意到如果 simulations_num = int(simulations)
不涉及,代码工作正常,但代码的其余部分(或类似的东西)是必要的
您需要 raise SystemExit()
,或者只使用 sys.exit()
(做同样的事情)。
您需要提出错误,或者至少使用 sys.exit(0)
。
工作原理是这样的:
import sys
if not input(': ').lower() in ['yes','y','1']:
sys.exit(0)
应用于你的例子就是这个
simulations = input("How many times would you like to run the simulation? ")
import sys
# Invalid answer, system exit
if (not simulations) or int(simulations) <=0:
print("That is not a valid number of simulations.")
print("Shutting down.")
sys.exit(0)
simulations_num = int(simulations)
#the rest of my code comes here
raise RuntimeError()
也可以
解决您的代码:
首先,您需要在SystemExit的前面加上“raise”关键字,它才能正常工作。 其次,要建立一个必须按Enter键才能让程序退出的情况,可以使用input()。
更正版本:
simulations = input("How many times would you like to run the simulation? ")
# Invalid answer, system exit
if (not simulations) or int(simulations) <= 0:
print("That is not a valid number of simulations.")
print("Shutting down.")
input("Press Enter to exit...")
raise SystemExit("Exiting... Reason: invalid input!")
simulations_num = int(simulations)
# the rest of my code comes here