Python pyscripter的输入取消异常?
当使用类似以下代码的程序时,无法正确执行 基本上,输入值必须是Python pyscripter的输入取消异常?,python,python-2.7,input,pyscripter,try-except,Python,Python 2.7,Input,Pyscripter,Try Except,当使用类似以下代码的程序时,无法正确执行 基本上,输入值必须是1、2、3、4或10,如果不是这些数字中的任何一个,则会让它们重试 如果他们选择了列表中的数字,那么它将继续到脚本的下一部分 在我运行时,它会弹出一个框供我输入值,但如果我按取消或红十字,它会发送“KeyboardInterrupt”异常,但我无法退出脚本/停止脚本运行 dvalues = [1, 2, 3, 4, 10] d1 = None while d1 is None: try: d1= int(in
1、2、3、4或10
,如果不是这些数字中的任何一个,则会让它们重试
如果他们选择了列表中的数字,那么它将继续到脚本的下一部分
在我运行时,它会弹出一个框供我输入值,但如果我按取消或红十字,它会发送“KeyboardInterrupt”异常,但我无法退出脚本/停止脚本运行
dvalues = [1, 2, 3, 4, 10]
d1 = None
while d1 is None:
try:
d1= int(input("First Value "))
if not d1 in dvalues:
d1= None
print("Can't do this")
except KeyboardInterrupt:
print('Canceled')
continue
else:
pass
有人知道我做错了什么吗
编辑
新的错误是,如果在第一次、第二次或任何输入时退出,它应该退出整个脚本,而不是继续
tot = int(15)
dvalues = [1, 2, 3, 4, 5, 10]
while tot > int(0):
d1 = None
while d1 is None:
try:
d1 = int(input("First Value"))
if not d1 in dvalues:
d1 = None
else:
print("Fail")
except KeyboardInterrupt:
break
d2 = None
while d2 is None:
try:
d2 = int(input("Second Value"))
if not d2 in dvalues:
d2 = None
else:
print("Fail")
except KeyboardInterrupt:
break
total = d1+d2
不要继续,尝试使用break。它将结束循环,而不是继续执行。
另外,else:pass部分是多余的。您现在可能已经发现了这一点,但您可以
import sys
sys.exit(1)
…提前退出脚本。exit()
接受错误代码,其中0
表示“无错误”编辑我的代码,以显示新错误。基本上,如果键盘中断/退出被调用,它需要退出整个脚本。。。