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")

24th May 2022, 3:12 AM
Ademir
1 Respuesta
+ 3
Your code is truncated because it's too big to fit in the post's Description. I suggest you to save it as Python code bit in SoloLearn and share its link in place of the raw text snippet for ease of analysis. https://www.sololearn.com/post/75089/?ref=app
24th May 2022, 5:42 AM
Ipang