The purpose of the following Python function is to search the details of a…
PYQ EMRS TGT/PGT (ESSE) 2025
The purpose of the following Python function is to search the details of a person in a binary file named contact.dat. The file stores records containing the Name, Address, Phone Number, and Email ID of multiple contacts. The function receives a person’s name as an argument and searches the file to display the corresponding contact details if the name matches. However, some parts of the program are missing. You are required to fill in the missing code segments labeled Blank A, Blank B, Blank C, and Blank D, so that the function performs its intended operation correctly.
_____ Blank A _____ # Line 01
def search_contact(Name): # Line 02
Found = False # Line 03
try: # Line 04
_____ Blank B _____ as File: # Line 05
while True: # Line 06
try: # Line 07
Data = _____ Blank C _____ # Line 08
if _____ Blank D _____: # Line 09
print("Name:", Data[0]) # Line 10
print("Address:", Data[1]) # Line 11
print("Phone:", Data[2]) # Line 12
print("Email:", Data[3]) # Line 13
Found = True # Line 14
break # Line 15
except EOFError: # Line 16
break # Line 17
if not Found: # Line 18
print("Contact not found.") # Line 19
except FileNotFoundError: # Line 20
print("Error! The file does not exist.") # Line 21(A) Write the missing code for Blank A to import the required module for processing binary files in Python.
(B) Write the missing code for Blank B to open the binary file contact.dat in the appropriate mode.
(C) Write the missing code for Blank C to read one record from the binary file.
(D) Write the missing code for Blank D to correctly compare the name entered by the user.
Attempted by 38 students.
Sign up free to check your answer
Sign up freeLoading lesson…

