Question
I am having trouble with the update_employee and search_by_ssn functions in my code. When I ran my code last week, it worked perfectly. This week,
I am having trouble with the update_employee and search_by_ssn functions in my code. When I ran my code last week, it worked perfectly. This week, I added export_employee and import_employee, but left everything else pretty much the same. What am I doing wrong? Here is my code.
employees = [] #creates a new list titled employee_list
employee_count = 0 #counter to track how many employees have been entered
def export_employee(employee):
fopen = open('employeedata.txt','a')
for i in employee:
fopen.write(i+',')
fopen.write(' ')
fopen.close()
def import_employee(employees):
temp = []
fopen = open('employeedata.txt','r')
for line in fopen:
line = line.strip()
if len(line) != 0:
line = line.split(',')
temp.append(line)
fopen.close()
return temp
employees = import_employee('employeedata.txt').copy()
for employee in employees:
employee_count +=1
def add_employee(): #defining a function to add employees
employee_name = input (' Enter employee name: ')
employee_phone = input ('Enter employee phone number, format (xxx)xxx-xxxx: ')
employee_ssn = input ('Enter employee SSN using no spaces or special characters: ')
employee_email = input ('Enter employee email address: ')
employee_salary = input('Enter employee salary: ')
employee_info = [] #creates a new list titled employee_info
employee_info.append(employee_name) #using the input information to append the employee info list
employee_info.append(employee_phone)
employee_info.append(employee_ssn)
employee_info.append(employee_email)
employee_info.append(employee_salary)
export_employee(employee)
return employee_info
def view_all_employees (employees): #defining a function to view all employees
if len(employees) == 0:
print (' There are no employees ') #tells the user that no employees are in the system
else:
print (' ')
#a for loop that will print out the employee information, by index location, until all employees have been reported
for employee in employees:
print(employee[0].center(45,'-')) #center-align with '-' padding
print('\tPhone:',employee[1])
print('\tSSN:', employee[2])
print('\tEmail:', employee[3])
print('\tSalary:','$'+employee[4]) #adding a '$' before the amount
print('-'*45) #'-' padding to match column width for first line
print(' ')
def search_by_ssn(employees, ssn): #define function to search by SSN
for index, employee in enumerate(employees):
if (employee[2] == ssn):
return index
return None
def update_employee(employees, index): #define function to update employee information
employees[index][0] = input(' Enter employee name: ')
employees[index][1] = input('Enter employee phone number: ')
employees[index][2] = input('Enter employee SSN: ')
employees[index][3] = input('Enter employee email: ')
employees[index][4] = input('Enter employee salary: ')
#a while loop to continually prompt the user to chose between adding and viewing employees
#loop will continue until the user chooses to exit the system by entering "3"
while True:
print ()
print ('There are', employee_count, 'employees currently in the system') #tells the user how many employees are in the system
print ('What would you like to do?')
print ()
print ('1. Add Employee 2. View All Employees 3. Search by SSN 4. Update employee info 5. Exit') #instructs user to make a selection
choice = int(input(' Enter a choice (1/2/3/4/5): '))
if choice == 1: #if the user selects 1, the system will run the add_employee function
info = add_employee()
employees.append(info)
employee_count +=1
print (" Employee's information added successfully ")
elif choice == 2: #if the user selects 2, the system will run the view_all_employees function
view_all_employees(employees)
elif choice == 3: #if user selects 3, the system will prompt for SSN and run search_by_ssn function
employee_ssn = input('Enter employee SSN: ')
index = search_by_ssn(employees, employee_ssn)
if(index is None):
print('Employee not found.')
else:
view_all_employees([employees[index]])
elif choice == 4: #if user selects 4, the system will prompt user for updated information for each line of information
employee_ssn = input('Enter employee SSN: ')
index = search_by_ssn(employees, employee_ssn)
update_employee(employees, index)
print('Employee successfully updated')
elif choice == 5: #if the user selects 5, the system prints a goodbye message and exits the program
print (' Goodbye')
break #breaks the loop once user selects 3
else: #if the user selects anything other than the available choices, the system will prompt the user to try again
print (' Error This is an invalid choice. Please try again... ')
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