Robust Calculator with Exception Handling
This challenge focuses on implementing exception handling in Python to create a robust calculator program. The goal is to build a simple calculator that can perform basic arithmetic operations (addition, subtraction, multiplication, and division) while gracefully handling potential errors like invalid input or division by zero. This exercise demonstrates how to write code that anticipates and manages unexpected situations, preventing crashes and providing informative feedback to the user.
Problem Description
You are tasked with creating a Python program that acts as a simple calculator. The program should:
- Prompt the user for two numbers and an operation to perform. The operation should be one of: '+', '-', '*', '/'.
- Perform the calculation based on the user's input.
- Handle potential exceptions:
TypeError: If the user enters non-numeric input for the numbers.ValueError: If the user enters a non-numeric string that can be converted to a number, but the conversion fails.ZeroDivisionError: If the user attempts to divide by zero.KeyError: If the user enters an invalid operation symbol (not '+', '-', '*', '/').
- Print the result if the calculation is successful.
- Print an appropriate error message if an exception occurs, informing the user about the problem. The error message should be user-friendly and explain what went wrong.
- Repeat the process until the user explicitly chooses to exit the program (e.g., by entering 'exit' as the operation).
Examples
Example 1:
Input:
Number 1: 10
Number 2: 5
Operation: +
Output:
Result: 15.0
Explanation: The program successfully adds 10 and 5, resulting in 15.
Example 2:
Input:
Number 1: 10
Number 2: 0
Operation: /
Output:
Error: Division by zero is not allowed.
Explanation: The program attempts to divide 10 by 0, triggering a ZeroDivisionError. The program catches the error and prints an informative message.
Example 3:
Input:
Number 1: abc
Number 2: 5
Operation: +
Output:
Error: Invalid input. Please enter numbers only.
Explanation: The user enters "abc" as the first number, which is not a valid number. This triggers a ValueError. The program catches the error and prints an appropriate message.
Example 4:
Input:
Number 1: 10
Number 2: 5
Operation: %
Output:
Error: Invalid operation. Please use +, -, *, or /.
Explanation: The user enters "%" as the operation, which is not a supported operation. This triggers a KeyError. The program catches the error and prints an appropriate message.
Constraints
- The program should handle both integer and floating-point numbers.
- The input numbers should be provided as strings.
- The program should be able to handle a large number of calculations without performance issues (though optimization is not the primary focus).
- The error messages should be clear and concise, guiding the user on how to correct their input.
Notes
- Consider using a
try-exceptblock to handle the potential exceptions. - You can use a
whileloop to allow the user to perform multiple calculations. - Think about how to provide a clear and user-friendly way to exit the program.
- The
KeyErrorcan be handled by checking if the operation is in a predefined set of valid operations. - Focus on providing informative error messages to the user.