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:
- What is the Input? (e.g., The total bill amount).
- What is the Process? (e.g., Calculate tax and tip).
- 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:
- Input: Total bill, Tip percentage, Number of people.
- Process:
- Find the tip amount (
bill * tip_percent / 100). - Add tip to bill.
- Divide total by number of people.
- Find the tip amount (
- 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:
- Ask the user for a temperature in Celsius.
- Ask if they want to convert to Fahrenheit or Kelvin.
- Use conditional statements to perform the correct math:
- Fahrenheit:
(Celsius * 9/5) + 32 - Kelvin:
Celsius + 273.15
- Fahrenheit:
- Print the result clearly.
Quick Knowledge Check
- What are the three stages of every computer program?
- How does pseudocode help you as a beginner?
- What does the DRY principle stand for?
- 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!