0

How to add error result to calculator in Python?

I followed the steps on how to make a calculator on the app, but now I want to add an "Error" result when a result goes past a certain number, like 100. Anyone know how to code that in? Thanks!

27th Nov 2018, 5:02 AM
Code Babylon
Code Babylon - avatar
6 Answers
+ 1
Can you please share the calculator's code so I can modify it? I might end up making something else otherwise 😅
27th Nov 2018, 6:43 AM
Harmeet Singh Bhatia
Harmeet Singh Bhatia - avatar
+ 1
Instead of the quotes, in the if statement, try enclosing the result in float(). John Boland
27th Nov 2018, 4:41 PM
Harmeet Singh Bhatia
Harmeet Singh Bhatia - avatar
0
Here is the code: while True: print("Options:") print("Enter 'add' to add two numbers") print("Enter 'subtract' to subtract two numbers") print("Enter 'multiply' to multiply two numbers") print("Enter 'divide' to divide two numbers") print("Enter 'quit' to end the program") user_input = input(": ") if user_input == "quit": break elif user_input == "add": num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) result = str(num1 + num2) print("The answer is " + result) if "result" > 100: print("Error") elif user_input == "subtract": num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) result = str(num1 - num2) print("The answer is " + result) elif user_input == "multiply": num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) result = str(num1 * num2) print("The answer is " + result) elif user_input == "divide": num1 = float(input("Enter a number: ")) num2 = float(input("Enter another number: ")) result = str(num1 / num2) print("The answer is " + result) else: print("Unknown Input") I tried adding an error section after the 'add' section, but I seemed to have miswrote the code
27th Nov 2018, 4:35 PM
Code Babylon
Code Babylon - avatar
0
Thank you, that helps a lot. I also found another way thanks to a friend of mine who took a CS class in school. Instead of if float(result), you can do if(num1 + num2) and that works as well.
27th Nov 2018, 4:44 PM
Code Babylon
Code Babylon - avatar
0
Well that works well but since you stored the result in another variable, it would be better to use that variable instead.
27th Nov 2018, 4:45 PM
Harmeet Singh Bhatia
Harmeet Singh Bhatia - avatar
0
That makes sense. Thank you.
27th Nov 2018, 6:13 PM
Code Babylon
Code Babylon - avatar