Week 1 Python Fundamentals - Basic syntax, variables, data types, and introductory programming exercises. Building foundation for Python development.
Create a simple Python program that:
- Asks the user to input two numbers.
- Asks the user to choose a mathematical operation (
+,-,*,/). - Performs the operation based on the user's input.
- Prints the result (e.g., inputs
10,5, and+→ output10 + 5 = 15).
This program solves the problem of taking user-provided numbers and an operator, then returning the calculated result in a clear, human-readable format.
- Practice reading input from the user.
- Convert text input to numeric types.
- Use conditional logic to decide which operation to run.
- Handle a common error case: division by zero.
- Format and display a clean result.
-
Prompt the user to enter the first number.
-
Prompt the user to enter the second number.
-
Prompt the user to enter the operation (
+,-,*,/). -
Branch logic based on the chosen operation:
- If
+→ add the two numbers. - If
-→ subtract the second from the first. - If
*→ multiply the two numbers. - If
/→- If the second number is not zero, divide the first by the second.
- If the second number is zero, show an error message (“Division by zero is not allowed!”).
- If
-
Print the result in the format:
first operation second = result -
If the user enters an invalid operation, show an error message.
- **Inputs**
- `num1`: first number (float)
- `num2`: second number (float)
- `operation`: one of `+`, `-`, `*`, `/`
- Output
- A single line showing the operation and result, e.g.
10 + 5 = 15
Enter the first number: 10 Enter the second number: 5 Enter the operation (+, -, *, /): + 10.0 + 5.0 = 15.0
Enter the first number: 12 Enter the second number: 7 Enter the operation (+, -, *, /): - 12.0 - 7.0 = 5.0
Enter the first number: 3 Enter the second number: 4 Enter the operation (+, -, *, /): * 3.0 * 4.0 = 12.0
Enter the first number: 8 Enter the second number: 2 Enter the operation (+, -, *, /): / 8.0 / 2.0 = 4.0
Enter the first number: 9 Enter the second number: 0 Enter the operation (+, -, *, /): / Error: Division by zero is not allowed!
Enter the first number: 5 Enter the second number: 5 Enter the operation (+, -, *, /): ^ Error: Invalid operation entered!
- Save the file as
calculator.pyin your project folder. - Open a terminal in that folder.
- Run:
python calculator.py
Follow the on-screen prompts.
- What the Code Covers (Mapping to Requirements)
-
Asks for two numbers → prompts for first and second number.
-
Asks for operation → prompts for +, -, *, or /.
-
Performs chosen operation → conditional (if/elif) selects the correct arithmetic.
-
Prints result in example format → shows num1 op num2 = result.
-
Division by zero → prints a clear error instead of crashing.
-
Invalid operator → prints a clear error.
-
Test Checklist (For Instructors & Students) 10, 5, + → 10 + 5 = 15
10, 5, - → 10 - 5 = 5 10, 5, * → 10 * 5 = 50 10, 5, / → 10 / 5 = 2 9, 0, / → error for division by zero 4, 2, ^ → error for invalid operation -
Non-numeric input (e.g., “abc”)
Input validation for numbers: re-prompt if the user types non-numeric input.
Loop & retry: let the user perform multiple calculations until they choose to exit.
Rounding: format results to a fixed number of decimal places.