You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Congratulations — you have learned all the fundamental building blocks of Python! Now it is time to combine everything into real, working programs. In this final lesson, you will build three mini projects that use variables, data types, strings, control flow, loops, lists, dictionaries, functions, file handling, and exception handling.
A command-line application that lets you add, search, list, and delete contacts, with data saved to a JSON file.
import json
import os
CONTACTS_FILE = "contacts.json"
def load_contacts() -> list[dict]:
"""Load contacts from the JSON file."""
if not os.path.exists(CONTACTS_FILE):
return []
try:
with open(CONTACTS_FILE, "r") as f:
return json.load(f)
except (json.JSONDecodeError, IOError):
print("Warning: Could not read contacts file. Starting fresh.")
return []
def save_contacts(contacts: list[dict]) -> None:
"""Save contacts to the JSON file."""
with open(CONTACTS_FILE, "w") as f:
json.dump(contacts, f, indent=2)
def add_contact(contacts: list[dict]) -> None:
"""Add a new contact."""
name = input("Name: ").strip()
if not name:
print("Name cannot be empty.")
return
phone = input("Phone: ").strip()
email = input("Email: ").strip()
# Check for duplicates
for contact in contacts:
if contact["name"].lower() == name.lower():
print(f"A contact named '{name}' already exists.")
return
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.