A simple python calculator i wrote
#!/usr/bin/python print("Please select your desire operation") print("1. Addition") print("2. Subtraction") print("3. Multiplication") print("4. Division") #this function is to add two numbers def add(a,b): x = a + b return x #this function is to subtract the two numbers def subs(a,b): x = a - b return x #this function is for multiplication def muls(a,b): x = a * b return x #this function is for division def divs(a,b): x = a / b return x try: insert = int(input("Enter your choice: ")) except (ValueError,KeyboardInterrupt): print ("You must type the number between 1 to 4") else: if insert == 1: a = int(input("Enter the first number to add: ")) b = int(input("Enter the second number to add: ")) print (add(a,b)) elif insert == 2: a = int(input("Enter the first number to subtract: ")) b = int(input("Enter the second number to subtract: ")) print (subs(a,b)) elif insert == 3: a = int(input("Enter the first number to multiply: ")) b = int(input("Enter the second number to multiply: ")) print (muls(a,b)) elif insert == 4: a = int(input("Enter the first number to divide: ")) b = int(input("Enter the second number to divide: ")) print (muls(a,b)) else: print ("Some unexpected error occurred")The first exception works like charm but when i am inside the if loop the program output's the error
Please select your desire operation 1. Addition 2. Subtraction 3. Multiplication 4. Division Enter your choice: 1 Enter the first number to add: 2 Enter the second number to add: abcd Traceback (most recent call last): File "Calculator3.py", line 33, in <module> b = int(input("Enter the second number to add: ")) ValueError: invalid literal for int() with base 10: 'abcd'How do i write a program which will accept exception error inside the if loop?