A beginner-friendly command-line calculator that accepts two user inputs, asks for an operation, and performs basic arithmetic using Python’s modern match-case control flow. Includes error handling for invalid inputs.
-
Accepts two integer inputs.
-
Supports:
- Addition (+)
- Subtraction (–)
- Multiplication (*)
- Division (/)
-
Uses Python’s
matchstatement for clean operation selection. -
Handles invalid numeric input via
try–except.
-
Prompts the user for two numbers (
aandb). -
Shows available operations.
-
Reads the operator.
-
Executes the matching arithmetic block:
match o: case "+": a + b
(Full logic in the script.)
-
Catches invalid input and prints an error message.
python main.pyEnter the first number: 8
Enter the second number: 3
what kind of operation do you want to perform...
Enter Operation: *
The result is: 24
Enter the first number: hi
Enter a valid value of a and b
- Python 3.10+ (for
match-case)
You can extend the calculator by:
- Adding modulus (
%) - Adding exponent (
**) - Adding input validation loops
- Converting it into a GUI or CLI tool