Question
Using python programming language complete the following two parts of the code In PART A , the program should display the records that are in
Using python programming language complete the following two parts of the code In PART A, the program should display the records that are in the employees.txt file content of employees.txt:
Ingrid Virgo 4587 Engineering Julia Rich 4588 Research Greg Young 4589 Marketing
The output of part A should be the following: Name: Ingrid Virgo ID: 4587 Dept: Engineering Name: Julia Rich ID: 4588 Dept: Research Name: Greg Young ID: 4589 Dept: Marketing
Code for part A:
# PART A
# This program displays the records that are
# in the employees.txt file
#Part A
def main():
# Open the employees.txt file.
emp_file =
# Read the first line from the file, which is
# the name field of the first record.
name = emp_file.
# If a field was read, continue processing.
while name != '':
# Read the ID number field.
id_num =
# Read the department field.
dept =
# Strip the newlines from the fields.
name =
id_num =
dept =
# Display the record.
print('Name:', name)
print('ID:', id_num)
print('Dept:', dept)
print()
# Read the name field of the next record.
# Close the file.
emp_file.close()
# Call the main function.
main()
In PART B, the program should allow the user to search the employees.txt file for records matching an employee name entered by the user. Please see the run sample below:
Sample Run 1: Enter an employee name to search for: Julia Rich name: Julia Rich ID: 4588 Dept: Research
Sample Run 3:
Enter an employee name to search for: Mohammad That employee was not found in the file.
Code for part B:
# PART B
# This program allows the user to search the
# employees.txt file for records matching a
# employee name.
def main():
# Create a bool variable to use as a flag.
found = False
# Get the search value.
search =
# Open the employee name file.
employee_file =
# Read the first record's name field.
name =
# Strip the from the name.
name =
# Read the rest of the file.
while #Insert the condition
# Read the ID field.
ID =
# Strip the from the ID.
ID =
# Read the dept field.
dept =
# Strip the from the dept.
dept =
# Determine whether this record matches
# the search value.
if #insert the condition here
# Display the record.
print('name:', name)
print('ID:', ID)
print('Dept:', dept)
print()
# Set the found flag to True.
# Read the next employee name.
# Strip the from the name.
# Close the file.
employee_file.close()
# If the search value was not found in the file
# display a message.
if #insert condition here
print('That employee was not found in the file.')
# Call the main function.
main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started