I have to write a menu-based 'Employee Management' program for one my assessment pieces. One of the main menu items is 'Employee Details', with the following options:
- Show employees
- Add new employees
- Remove employees
I've already completed 1, & 2, but I'm stuck with a specific function in 3. Remove employees. When an employee is added, it's stored in the employees_all
dictionary as such:
{1: {'fullname': 'John Smith', 'phonenumber': '0400 000 000', 'designation': 'Technician'},
2: {'fullname': 'Jane Doe', 'phonenumber': '0411 111 111', 'designation': 'Manager'},
3: {'fullname': 'Jimmy Smithers', 'phonenumber': '0422 222 222', 'designation': 'Maintenance'}}
I deliberately assigned an ascending int key when an employee is added. To remove an employee, program takes the employee id (the key) as input, and deletes that employee from the dictionary.
What I want, is that when (for instance) employee #2 is deleted, I want employee #3's key to become 2, for employee #4's key to become 3, etc, so on and so forth. I've tried to create some for
loops that subtract 1 from dictionary keys greater than the value of the deleted employee's key, but I keep getting: unsupported operand type(s) for -: 'dict' and 'int'
Each menu item has it's own function. For this one, it's remove_employee()
. See code below:
def remove_employee():
print("\n______________________________________________")
print("\n> MAIN MENU > EMPLOYEE DATA > REMOVE EMPLOYEE")
print("\n\nWhich employee would you like to delete?")
delnum = int(input("\n\nEmployee Number: "))
print("\n______________________________________________")
print("\n\nARE YOU SURE YOU WANT TO DELETE EMPLOYEE #",delnum,"?")
print("\n1. Yes\n2. No")
areyousure1 = input("\nEnter choice: ")
if areyousure1 == "1":
del(employees_all[delnum])
#### PLEASE HELP HERE ####
print("\n______________________________________________")
print("\nEMPLOYEE DATA SUCCESSFULLY DELETED")
time.sleep(1)
submenu1()
elif areyousure1 == "2":
print("\n______________________________________________")
print("\n--------- EMPLOYEE DATA NOT DELETED ----------")
time.sleep(1)
submenu1()
else:
print("\n______________________________________________")
print("\nINVALID INPUT! PLEASE ENTER A VALID NUMBER.")
time.sleep(1)
remove_employee()