I am Trying to execute a script in which if non integer value is enter is should catch with except but it doesn't seem to work ..in my case print(message) in except block is showing in output while executing but errors also come
print('Answer is :', add(num1, num2))
NameError: name 'num1' is not defined
I'm using python 3.8... kindly assist :
def add(num1, num2):
return num1 + num2
def subtract(num1, num2):
return num1 - num2
def into(num1, num2):
return num1 * num2
def by(num1, num2):
return num1 / num2
print("Select operation :")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
choice = input("Enter choice(1/2/3/4): ")
if choice in ["1", "2", "3", "4"]:
try:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
except NameError:
print('Only Integers Allowed')
except ValueError:
print('Only Integers Allowed ')
else:
print("Invalid Input")
exit()
def calci():
if choice == "1":
print('Answer is :', add(num1, num2))
elif choice == "2":
print('Answer is :', subtract(num1, num2))
elif choice == "3":
print('Answer is :', into(num1, num2))
elif choice == "4":
print('Answer is :', by(num1, num2))
calci()