Skip to main content

Mini contact diary

crate a dictionary and function to add new data





Data store:

This line creates an empty dictionary named data_store.
A dictionary stores data in key-value pairs.


def add_data()

This line defines a function named add_data

This function can be called later to perform a task in this case, to add a name and mobile number to the dictionary.


data_store[key] = value:

This line adds the new name and mobile number to the dictionary.


save_data()

This calls the save_data() function not shown in your code here but assumed to be defined else where.



def search_data():

This defines a function called search_data.

This function can be called later in the program when you want to look up information by keyword.

if query in data_store:


This checks if the entered keyword (query) exists in the dictionary data store.

If it does exist, the program will continue to the next line inside the if block.

If not, it will skip to the else part.




def save_data():

This defines a function named save_data.

It has an optional parameter called filename which defaults to data.txt if no other filename is provided.

This function is responsible for saving the current data stored in the data_store dictionary into a file.

 try:

This starts a try block — meaning the code inside it will be attempted.

If any error occurs while trying to save the file, Python will jump to the except block to handle it.

with open(filename, "w") as file:

This line opens the file named filename (default: "data.txt") in write mode ("w").

Write mode will overwrite the file if it already exists.

with ensures that the file is automatically closed after the code block finishes running.

file is a variable that represents the open file object.



def load_data():

This defines a function named load_data.

It takes one optional argument, filename, which defaults to "data.txt" if not provided.

The purpose of this function is to load existing data from the file into the program.

except FileNotFoundError:

This block runs if the file does not exist when running the program for the first time.

It tells the user: “No previous data file found. Starting fresh.



load_data()

This line is calling a function named load_data().

Its probably used to load existing data from a file or a database into the program before starting anything else.

We can't see the definition of load_data() but its assumed to exist elsewhere in the code.

while True:

This starts an infinite loop.

While True means the loop will keep running forever unless you explicitly stop it usually with a break statement.

This is commonly used for menu driven programs so the user can interact multiple times.



choice = input("Enter your choice (1/2/3): ").strip()

This line asks the user to enter a choice: either 1, 2, or 3.

Input takes the user's input as a string.

Strip() removes any extra spaces before or after the input. For example  if the user enters " 1 " it will become "1".


Comments

Popular Posts

A beginner's guide to HTTP/1 vs. HTTP/2

HTTP, is a foundational protocol for communication on the World Wide Web. It defines how messages are formatted and transmitted between web clients and web servers, enabling the retrieval of web pages, images, videos, and other resources. Essentially, it's the language that web browsers and servers use to "talk" to each other.  What is HTTP? The Postman Analogy HTTP, or Hyper Text Transfer Protocol , is the fundamental set of rules that allows your web browser and a website's server to "talk" to each other. Think of it like this: You are the person who wants a package.   The Postman is your web browser. The Store is the website’s server (e.g., google.com ). When you want to visit a website, your browser uses HTTP to tell the server what you want. The server then finds the information and sends it back to your browser, which displays it for you. HTTP/1 The first major version of the Hyper Text Transfer Protocol , which is the basic system that web browsers...

From Code to Cloud - Understanding Hosting 🌐

Hosting (or Web Hosting) is a service that allows individuals or organizations to store their website files (HTML, CSS, images, videos, etc.) on a server connected to the internet so that anyone in the world can access the website via a domain name (like www.google.com).  Ever wondered how a website like youtube.com stays online 24/7 for anyone in the world to visit? The answer is hosting . It’s the essential service that makes your website visible on the internet and accessible to everyone, everywhere.  To understand it, let’s use a simple and relatable analogy: building and living in a house. The Landlord, the Land, and the House   Imagine your website is a house you want to build and open for visitors. The Website is the House: Your website is a collection of all its different parts. The front door is your homepage, the rooms are the other pages, and the furniture, decor, and art are all the files that give your site its look and feel. These are files like index....

Top 3 GitHub Features Every Beginner Should Master

Introduction If you’re just starting your journey as a developer, GitHub can feel a bit overwhelming. It’s packed with tools that professionals use every day but which ones should you focus on first? You don’t need to learn everything at once. Instead, start with the features that will give you the biggest benefits right away: safety, organization, and automation. Here are my top 3 GitHub features that every beginner should master and exactly why they matter. 1. Branches & Pull Requests Branches let you create  a copy of your project to try out new changes without ever touching the main, working code. It's like having a clone of your project where you can experiment freely.  Pull Requests   are how you propose merging those new changes back into the main project. They serve as a formal request to "pull" your changes from your branch into the main one, often after a review process. Benefits : You can experiment without fear of "breaking"...