Skip to content
Back to course

8. Build a small project

You have learned variables, if/else conditions, loops, and functions. Now it is time to combine all of them into one real, working program. In this final lesson you will plan and build a mini expense-tracker: a program that lets Abebe record daily spending at his small shop in Addis Ababa, see the total, and check whether he is over budget. By the end you will have a complete Python project you understand line by line.

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.

A dictionary in Python stores key-value pairs inside curly braces. Example: expense = {"name": "flour", "amount": 150} print(expense["name"]) # flour print(expense["amount"]) # 150 Think of a dictionary like a labelled envelope: the key is the label, the value is what's inside. We use a list of dictionaries here so each expense has both a name and an amount stored together.

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?

Course recap — you have now used every skill from this course in one project: • Variables: stored budget, expense name, amount, and total • Input / output: print() and input() let the program talk to the user • Conditions (if/else): checked whether the total was over or under budget • Loops (while): kept collecting expenses until the user typed 'done' • Functions: calculate_total, show_summary, check_budget, main each do one clear job • Lists and dictionaries: stored a growing collection of structured expense records • Planning: you broke the problem into steps before writing a single line of code Congratulations — you have completed the Python for Beginners course. You can now read and write basic Python, and you have built a real working program. Keep experimenting, keep building!

Check your understanding

1/7 · 80 XP

What is the purpose of planning (writing down features, data, and steps) before coding a project?