You can use an exception to achieve this.
With the code that you have used, I would do this.
If I was writing a calculator, I would just write something like this:
I hope this helps!
With the code that you have used, I would do this.
print("Select An Operator") print("Addition: +") print("Subtraction: -") print("Multiplication: *") print("Division: /") operator = input("Enter Choice (+,-,*,/:) ") number = int(input("Please enter a number: ")) number2 = int(input("Please enter a second number: ")) try: if operator == '+': print(number + number2) elif operator == '-': print(number - number2) elif operator == '*': print(number * number2) elif operator == '/': print(number / number2) else: print("ERROR!") except: print("ERROR!")This code will "try" to do the task, and will output "ERROR" if an error is returned as we have used an exception.
If I was writing a calculator, I would just write something like this:
try:print(eval(input("Enter the equation"))) except:print("Error")It takes input like 5*3, evaluates it and outputs the result. If there is an error in the process, "Error" will be outputted.
I hope this helps!