This function of my program is searching a file that is formatted as first_name last_name rate hours:
John Smith 25 80
Mary Johnson 15.5 60.0
...
With one space between each item.
The user enters the first name and last name of the employee and while the line is not equal to null it searchs for the employee. If its found then found=True and it gets the rate and hours from that line and breaks out of the loop. Then if found=True, it will print the name of the employee and their gross pay (rate * hours), else it says employee is not found. The code is working in my program but I will be using something very similar later on for adding/modifying/deleting new employee functions.
# 'print_emp', display only a single employee's data chosen by the user displayed as
# firstname, lastname, grosspay (on one line of output)
def print_emp():
menu_name = ' '*int(OFFSET/2) + "EMPLOYEE LOOKUP"
dotted = (OFFSET+len(menu_name))*'-'
found = False
try:
file= open('employees.txt')
line = file.readline()
name = input('Give me an employee to search for ')
# run thru while loop & search for employee
while line != ' ':
line = line.split(' ') #split into a list
if name == line[0]+' '+line[1]:
found = True
rate = float(line[2])
hours = float(line[3])
break
line = file.readline() # read subsequent records
# close file
file.close()
if found:
print(name, '$%4.2f' % (rate*hours))
else:
print('Employee is not listed')
except FileNotFoundError:
print("Error: File not found.")