Skip to content

Week 1 Python Fundamentals - Basic syntax, variables, data types, and introductory programming exercises. Building foundation for Python development.

Notifications You must be signed in to change notification settings

Gideon-Kipngeno/wk-1-python-Gideon-Kipngeno

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

wk-1-python-Gideon-Kipngeno

Week 1 Python Fundamentals - Basic syntax, variables, data types, and introductory programming exercises. Building foundation for Python development.

Basic Calculator — README

1) Assignment Brief (What to Build)

Create a simple Python program that:

  1. Asks the user to input two numbers.
  2. Asks the user to choose a mathematical operation (+, -, *, /).
  3. Performs the operation based on the user's input.
  4. Prints the result (e.g., inputs 10, 5, and + → output 10 + 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.

2) Learning Goals (Why This Matters)

  • 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.

3) How the Program Works (Step by Step)

  1. Prompt the user to enter the first number.

  2. Prompt the user to enter the second number.

  3. Prompt the user to enter the operation (+, -, *, /).

  4. 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!”).
  5. Print the result in the format:
    first operation second = result

  6. If the user enters an invalid operation, show an error message.

4) Inputs and Outputs

- **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

5) Example Runs

Example A — Addition

Enter the first number: 10 Enter the second number: 5 Enter the operation (+, -, *, /): + 10.0 + 5.0 = 15.0

Example B — Subtraction

Enter the first number: 12 Enter the second number: 7 Enter the operation (+, -, *, /): - 12.0 - 7.0 = 5.0

Example C — Multiplication

Enter the first number: 3 Enter the second number: 4 Enter the operation (+, -, *, /): * 3.0 * 4.0 = 12.0

Example D — Division

Enter the first number: 8 Enter the second number: 2 Enter the operation (+, -, *, /): / 8.0 / 2.0 = 4.0

Example E — Division by Zero (Handled)

Enter the first number: 9 Enter the second number: 0 Enter the operation (+, -, *, /): / Error: Division by zero is not allowed!

Example F — Invalid Operation (Handled)

Enter the first number: 5 Enter the second number: 5 Enter the operation (+, -, *, /): ^ Error: Invalid operation entered!

6) How to Run the Program

  1. Save the file as calculator.py in your project folder.
  2. Open a terminal in that folder.
  3. Run:
    python calculator.py
    
    

Follow the on-screen prompts.

  1. 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.

  1. 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
    
  2. 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.

About

Week 1 Python Fundamentals - Basic syntax, variables, data types, and introductory programming exercises. Building foundation for Python development.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages