In the following script:
# 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)
I want "Please enter a number" to be displayed if the input for num is not an integer value, as that gives ValueError due to int(num) found inside mult().
However, the try/except block doesn't seem to work because, when entering a non-numerical string, it displays Python's Traceback error:
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'
Why is error handling not working?