Project Overview 💡
In this project, you’ll build a basic contact list manager using Python. The app allows users to enter names and phone numbers and saves the list to a text file. This project helps reinforce concepts like loops, lists, string formatting, and file I/O.
Challenge Yourself! 🚀
Before checking the solution, try building a script that collects contact information from the user and saves it to a file.
Task:
Write a Python program that:
Prompts the user to input contact names and phone numbers
Stores each contact as a name-number pair
Saves the list to a text file when the user finishes entering contacts
Expected Output: The program prompts the user to enter names and phone numbers through the terminal:
If the user enters “done” the program ends and saves the data in a contacts.txt file which looks like the following:
Give it a shot! Scroll down when you're ready for the step-by-step guide.
Spoiler Alert!
Step-by-Step Guide
Step 1️⃣: Create an Empty List
Start by defining an empty list to store the contacts.
contacts = []
Step 2️⃣: Start a Loop for Input
Use a while True
loop to continuously prompt the user for contact information.
while True:
name = input("Enter contact name (or type 'done' to finish): ").strip()
if name.lower() == 'done':
break
phone_number = input(f"Enter phone number for {name}: ").strip()
contacts.append(f"{name}: {phone_number}")
Step 3️⃣: Save Contacts to a File
Write each contact from the list to a text file.
with open("contacts.txt", "w") as file:
for contact in contacts:
file.write(contact + "\n")
Step 4️⃣: Confirm Completion
Print a message to notify the user that contacts have been saved.
print("Contacts saved to 'contacts.txt'.")
Complete Code 🧨
# Simple Contact List Manager
contacts = []
while True:
# Prompt for contact name
name = input("Enter contact name (or type 'done' to finish): ").strip()
if name.lower() == 'done':
break
# Prompt for contact phone number
phone_number = input(f"Enter phone number for {name}: ").strip()
# Add the contact to the list
contacts.append(f"{name}: {phone_number}")
# Write the contacts to a text file
with open("contacts.txt", "w") as file:
for contact in contacts:
file.write(contact + "\n")
print("Contacts saved to 'contacts.txt'.")
Alternative Solution
You can use a dictionary instead of a list for better structure and easier lookups:
contacts = {}
while True:
name = input("Enter contact name (or type 'done' to finish): ").strip()
if name.lower() == 'done':
break
phone_number = input(f"Enter phone number for {name}: ").strip()
contacts[name] = phone_number
with open("contacts.txt", "w") as file:
for name, phone_number in contacts.items():
file.write(f"{name}: {phone_number}\n")
print("Contacts saved to 'contacts.txt'.")
Comparison
The original version uses a list of formatted strings, which is simple and effective for small projects.
The alternative version uses a dictionary, which is more flexible if you plan to add features like searching or deleting contacts.
Want More? 🔥
Enjoyed this project? Unlock real-world projects with full guides & solutions by subscribing to the paid plan. 🎉