8. Build a small project
Good programmers plan before they type. Planning means answering three questions:
1. What should the program do? (features)
2. What data does it need? (variables / data structures)
3. What steps does it take? (algorithm)
Our mini expense-tracker should:
• Ask the user to enter expense names and amounts one at a time
• Keep running until the user types "done"
• Show a list of all expenses at the end
• Calculate and print the total amount spent
• Compare the total against a budget and tell the user whether they are under or over budget
Data we need:
• A list to store each expense as a dictionary (name + amount)
• A variable for the budget limit
Steps:
1. Set the budget
2. Collect expenses in a loop
3. Calculate the total
4. Show the summary
5. Check the budget
Breaking a program into steps like this is called algorithmic thinking — one of the most important skills in programming.
We will build the project function by function. This approach is called top-down development: plan the big picture first, then fill in the details.
Step 1 — a function to calculate the total from the expense list:
def calculate_total(expenses):
total = 0
for expense in expenses:
total = total + expense["amount"]
return total
expenses is a list of dictionaries. Each dictionary has two keys: "name" and "amount". The loop adds up every amount and returns the total.
Step 2 — a function to display the expense list neatly:
def show_summary(expenses):
print("\n--- Expense Summary ---")
for expense in expenses:
print(expense["name"], ":", expense["amount"], "birr")
print("-----------------------")
Step 3 — a function to check the budget:
def check_budget(total, budget):
if total <= budget:
remaining = budget - total
print("Under budget! You have", remaining, "birr left.")
else:
over = total - budget
print("Over budget by", over, "birr. Be careful!")
Each function does exactly one job. This makes the code easy to read, test, and fix.
Now we write the main part — the part that talks to the user:
def main():
print("Welcome to Abebe's Shop Expense Tracker")
budget = float(input("Enter your daily budget in birr: "))
expenses = []
while True:
name = input("\nExpense name (or 'done' to finish): ")
if name.lower() == "done":
break
amount_str = input("Amount in birr: ")
amount = float(amount_str)
expense = {"name": name, "amount": amount}
expenses.append(expense)
print("Added:", name, "-", amount, "birr")
if len(expenses) == 0:
print("No expenses recorded.")
else:
show_summary(expenses)
total = calculate_total(expenses)
print("\nTotal spent:", total, "birr")
check_budget(total, budget)
main()
Key points in the main function:
• float(input(...)) converts the user's text to a decimal number
• while True creates an infinite loop — we break out of it when the user types "done"
• name.lower() converts the input to lowercase so "Done", "DONE", and "done" all work
• expenses.append(expense) adds each new expense dictionary to the list
• len(expenses) == 0 checks if no expenses were entered before trying to calculate
Here is the complete program from top to bottom, ready to run:
def calculate_total(expenses):
total = 0
for expense in expenses:
total = total + expense["amount"]
return total
def show_summary(expenses):
print("\n--- Expense Summary ---")
for expense in expenses:
print(expense["name"], ":", expense["amount"], "birr")
print("-----------------------")
def check_budget(total, budget):
if total <= budget:
remaining = budget - total
print("Under budget! You have", remaining, "birr left.")
else:
over = total - budget
print("Over budget by", over, "birr. Be careful!")
def main():
print("Welcome to Abebe's Shop Expense Tracker")
budget = float(input("Enter your daily budget in birr: "))
expenses = []
while True:
name = input("\nExpense name (or 'done' to finish): ")
if name.lower() == "done":
break
amount = float(input("Amount in birr: "))
expenses.append({"name": name, "amount": amount})
print("Added:", name)
if len(expenses) == 0:
print("No expenses recorded.")
else:
show_summary(expenses)
total = calculate_total(expenses)
print("\nTotal spent:", total, "birr")
check_budget(total, budget)
main()
Sample run (user input shown with >):
Welcome to Abebe's Shop Expense Tracker
Enter your daily budget in birr: > 500
Expense name (or 'done' to finish): > flour
Amount in birr: > 150
Added: flour
Expense name (or 'done' to finish): > oil
Amount in birr: > 200
Added: oil
Expense name (or 'done' to finish): > sugar
Amount in birr: > 180
Added: sugar
Expense name (or 'done' to finish): > done
--- Expense Summary ---
flour : 150.0 birr
oil : 200.0 birr
sugar : 180.0 birr
-----------------------
Total spent: 530.0 birr
Over budget by 30.0 birr. Be careful!
Scenario
Sara is adding expenses: flour (100 birr), oil (200 birr), soap (50 birr). Her budget is 300 birr. After she types 'done', what will the program print about her budget?
Check your understanding
1/7 · 80 XPWhat is the purpose of planning (writing down features, data, and steps) before coding a project?