This Python program reads employee data in a loop:
- For each employee, it reads their ID and overtime hours
- The input ends when the user enters 99 as the employee ID
- The program then displays the three employees with the least overtime hours
# List to store employee data
employees = []
while True:
emp_id = int(input("Enter employee ID (99 to exit): "))
if emp_id == 99:
break
overtime = float(input(f"Overtime hours for employee {emp_id}: "))
employees.append((emp_id, overtime))
# Sort by overtime hours
employees.sort(key=lambda x: x[1])
# Display top 3 with least overtime
print("\nThree employees with the least overtime:")
for emp in employees[:3]:
print(f"Employee ID: {emp[0]} | Overtime: {emp[1]}")
Enter employee ID (99 to exit): 101
Overtime hours for employee 101: 12.5
Enter employee ID (99 to exit): 102
Overtime hours for employee 102: 8
Enter employee ID (99 to exit): 103
Overtime hours for employee 103: 15
Enter employee ID (99 to exit): 104
Overtime hours for employee 104: 6
Enter employee ID (99 to exit): 99
Three employees with the least overtime:
Employee ID: 104 | Overtime: 6.0
Employee ID: 102 | Overtime: 8.0
Employee ID: 101 | Overtime: 12.5
- The program loops until 99 is entered
- Each employee’s ID and overtime hours are stored as a tuple
- The list is sorted by overtime hours using lambda
- The first three entries are printed as the lowest overtime workers