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:
data store.
if block.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
Post a Comment