0

I am working on an Employee Management System and would like my user to search for an employee by SSN. Once the employee is found, I'd like to print the list associated with that SSN. Eventually, I'd like to search for an employee by SSN and allow the user to edit the data in the fields as well. Right now, I can't figure out how to associate the SSN search to the list. I can find the SSN, but I don't know how to display it. How do I do that?

employee_info = [ ]

while True: # While loop created to make the script constantly run
    counter = len(employee_info) # Counter created so that the user will know how many entries there are

    print('There are', '(', (int(counter)), ')', 'employees in the system.\n')

    add_new = input('Would you like to add a new employee to the system, find an employee\'s record by SSN, change an employee\'s information or view all employee entries in the system?\
    To add an employee, type: "ADD". To view all employees currently in the system, type: "VIEW ALL." To find an employee, type: "FIND." To change employee info, type: "CHANGE."\n'\
    ) #Added option to add an employee, find an employee by SSN, change information or to view all employees currently the system

    if add_new == 'ADD':
        while True: # Loop created to input employees with option to quit
            employee_index = [input('Employee Name\n'), input('Employee SSN\n'), \
            input('Employee Telephone Number ***Entered as (555)555-5555*** \n'), input('Employee Email\n'), input('Employee Salary ***Entered as $xxxx***\n')] 
            employee_info.append(employee_index)
            more_employees = input('Would you like to add another employee? Y or N.\n')
            if more_employees == 'Y':
                continue
            elif more_employees == 'N':
                break

    elif add_new == 'VIEW ALL':
        for employee_index in employee_info:
            print('            -----------------', employee_index[0], '-----------------\n')
            print('SSN:', employee_index[1], '\n')
            print('Phone:', '(' + employee_index[2][0] + employee_index[2][1] + employee_index[2][2] + ')' + employee_index[2][3] + employee_index[2][4] + employee_index[2][
            5] + '-' + employee_index[2][6] + employee_index[2][7] + employee_index[2][8] + employee_index[2][9], '\n') 
            print('Email:', employee_index[3], '\n')
            print('Salary:', '$' + str(employee_index[4]), '\n')
            print('            ----------------------------------------------------')

    elif add_new == "FIND":
        find_emp = input('Please enter the employee SSN in the following format: 333221111.\n')
        if find_emp in employee_index:
Hydreaux
  • 13
  • 3
  • Does this answer your question? [Finding the index of an item given a list containing it in Python](https://stackoverflow.com/questions/176918/finding-the-index-of-an-item-given-a-list-containing-it-in-python) – mrkre Feb 18 '20 at 06:17

3 Answers3

0

Instead of a dict try using key pair values like this

mydict = {"SSN": 465736283, "Name": 'Some Dude'}

But have it like this

{
    "employee": {
        "SSN": "1574083",
        "username": "gustog",
        "full_name": "Don Won",
        "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg",
        "bio": "This is my bio",
        "website": "http://donwon.gov",
        "counts": {
            "media": 1320,
            "follows": 420,
            "followed_by": 3410
        }
}

This might let you store the info better and then retrieved it faster instead of looping over a array

MNM
  • 2,673
  • 6
  • 38
  • 73
0

You can parse through every employee in the list, and if employee[1] == find_emp i.e. if the serial number matches, you can print the employee's details.

elif add_new == "FIND":
    find_emp = input('Please enter the employee SSN in the following format: 333221111.\n')
    found = False
    for employee in employee_info:
        if employee[1] == find_emp:
            print('            -----------------', employee_index[0], '-----------------\n')
            print('SSN:', employee_index[1], '\n')
            print('Phone:', '(' + employee_index[2][0] + employee_index[2][1] + employee_index[2][2] + ')' + employee_index[2][3] + employee_index[2][4] + employee_index[2][
            5] + '-' + employee_index[2][6] + employee_index[2][7] + employee_index[2][8] + employee_index[2][9], '\n') 
            print('Email:', employee_index[3], '\n')
            print('Salary:', '$' + str(employee_index[4]), '\n')
            print('            ----------------------------------------------------')
            found = True

    if found == False:
        print("Employee not found!")

If the employee is not found, you print the appropriate error message.

Robo Mop
  • 3,485
  • 1
  • 10
  • 23
0

Looks like you're reading in a list of employee indexes, and each employee index is a list of five elements, with the second of five being the SSN. So you could run through the list of 5-item-lists, checking the 2nd of the 5 for a match to the SSN. So...

def match_the_ssn(list_of_lists, ssn):
    for item in list_of_lists:
        try:
            if item[1] == ssn:
                print(item)
                return item
        except:
            pass
    return

Something along these lines.... Run through the list, looking at each sublist. For each sublist, check if sublist[1] is the ssn you want. If so, you've got the sublist right there to do with what you want.

GaryMBloom
  • 5,350
  • 1
  • 24
  • 32