Lesson 9: Writing Simple Programs
·Programming

Lesson 9: Writing Simple Programs

Learn the art of problem-solving and how to combine variables, logic, and loops into functional Python scripts.

Lesson 9: Writing Simple Programs

You now have all the primary tools of a Python programmer: Variables, Input/Output, Operators, Conditionals, and Loops. But having a hammer and a saw doesn't mean you know how to build a house. In this lesson, we focus on the Process of writing a program.

Lesson Overview

In this lesson, we will cover:

  • Decomposition: How to break a big problem into small pieces.
  • Pseudocode: Writing code in "human" first.
  • The DRY Principle: "Don't Repeat Yourself."
  • Case Study: Building a "Tip Calculator" from scratch.

1. Problem Decomposition

When faced with a coding task, don't just start typing. Ask yourself:

  1. What is the Input? (e.g., The total bill amount).
  2. What is the Process? (e.g., Calculate tax and tip).
  3. What is the Output? (e.g., The final amount per person).

2. Using Pseudocode

Pseudocode is a plain English version of your code. It helps you design logic without worrying about syntax.

Example for a Guest List program:

Ask for the guest's name.
If name is on the allowed list:
    Print "Welcome!"
Else:
    Print "Identify yourself!"

3. The DRY Principle (Don't Repeat Yourself)

If you find yourself copying and pasting the same code three times, there is probably a better way to do it using a Loop or a later concept called Functions. DRY code is easier to maintain and has fewer bugs.


4. Case Study: The "Split the Bill" App

Let's build a program that calculates how much each person should pay at dinner, including tip.

The Plan:

  1. Input: Total bill, Tip percentage, Number of people.
  2. Process:
    • Find the tip amount (bill * tip_percent / 100).
    • Add tip to bill.
    • Divide total by number of people.
  3. Output: Final cost per person.

The Code:

print("--- Welcome to the Bill Splitter ---")

# Step 1: Gather Input
total_bill = float(input("What was the total bill? $"))
percentage = int(input("How much tip would you like to give (10, 15, or 20)? "))
people = int(input("How many people to split the bill? "))

# Step 2: Perform Calculations
tip_amount = total_bill * (percentage / 100)
final_total = total_bill + tip_amount
price_per_person = final_total / people

# Step 3: Show Output
print(f"\nFinal Bill (with tip): ${final_total:.2f}")
print(f"Each person should pay: ${price_per_person:.2f}")

Note: :.2f inside the f-string tells Python: "Show only 2 digits after the decimal point."


Practice Exercise: The Temperature Converter

Create a file named temp_converter.py. Your program should:

  1. Ask the user for a temperature in Celsius.
  2. Ask if they want to convert to Fahrenheit or Kelvin.
  3. Use conditional statements to perform the correct math:
    • Fahrenheit: (Celsius * 9/5) + 32
    • Kelvin: Celsius + 273.15
  4. Print the result clearly.

Quick Knowledge Check

  1. What are the three stages of every computer program?
  2. How does pseudocode help you as a beginner?
  3. What does the DRY principle stand for?
  4. Why is problem decomposition important?

Key Takeaways

  • Break big problems into small, manageable steps.
  • Plan your logic in plain English (pseudocode) before coding.
  • Avoid repeating code whenever possible.
  • Always consider your Input, Process, and Output.

What’s Next?

Every programmer makes mistakes. In Lesson 10, we’ll look at Common Beginner Mistakes and learn how to debug them like a pro so you don't get stuck!

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn