在以下脚本中:
# shows multiplication table of a number upto that number
import time
while True:
num = input("Enter number: ")
try:
def mult():
for i in range(1, int(num)+1):
print(str(i) + " x " + num + " = " + str(i * int(num)))
except ValueError:
print("Please enter a number")
continue
mult()
time.sleep(2)
如果输入的不是整数值,我希望显示“请输入数字” ,因为在内部找到了。numValueErrorint(num)mult()
但是,try/except 块似乎不起作用,因为在输入非数字字符串时,它会显示 Python 的 Traceback 错误:
Enter number: forty five
Traceback (most recent call last):
File "...", line 13, in <module>
mult()
File "...", line 7, in mult
for i in range(1, int(num)+1):
ValueError: invalid literal for int() with base 10: 'forty five'
为什么错误处理不起作用?
白板的微信
慕无忌1623718
相关分类