Help with Try Except for my calculator | Sololearn: Learn to code for FREE!

0

# Help with Try Except for my calculator

Hello, my calculator is viewable on my profile but I'll post code here as well. Essentially, I am having issues with my try/except for my calculator. If my input is something like: a + a I would like for my except print statement to run, which it won't. It just give ValueError and its own print error back. I would also like my code to stop running if the except it run. Appreciate help thx. CODE: # The input ex. 2 + 2 prob = input() # Using operator for math import operator import math # Converting the input into a list prob_list = list(prob.split(" ")) # Checking if the input is an integer. try: isinstance(prob_list[0], int) == True isinstance(prob_list[2], int) == True except: print("Please enter a number.") # Functions to be called for the math def add(x, y): return operator.add(x, y) def subt(x, y): return x - y def mult(x, y): return x * y def divi(x, y): return x / y def powr(x, y): return x ** y def sqrt(x): return math.sqrt(x) # Statements to be sent off to the functions above if prob_list[1] == "+": print(add(int(prob_list[0]), int(prob_list[2]))) if prob_list[1] == "-": print(subt(int(prob_list[0]), int(prob_list[2]))) if prob_list[1] == "*": print(mult(int(prob_list[0]), int(prob_list[2]))) if prob_list[1] == "**": print(powr(int(prob_list[0]), int(prob_list[2]))) if prob_list[1] == "sqrt": print(sqrt(int(prob_list[0]))) # Division. Will also handle the error if divding by 0 if prob_list[1] == "/": try: print(divi(int(prob_list[0]), int(prob_list[2]))) except ZeroDivisionError: print("Divided by zero")

5/24/2022 3:12:39 AM