tryin to write a function that searches for SSN in a dict, and if that SSN is found, to retrieve all the data associated with that SSN

23 views Asked by At

I am trying to figure out how to retrieve the rest of the data associated with the found SSN (name, phone, email, salary). Once I am able to pull that data based on the matched SSN, then i want to offer the chance to edit it. But i cant figure out how to match the SSN and retrieve the info associated with it.

num_employees = 0
employee = {}
#for clearing the shell
def cls():
    print("\n"*2)

#add new employee
def add_employee():
    cls()
    global num_employees
    employee_name = input('Please input employee first and last name: ')
    employee_SSN = input("Enter employee SSN: ")
    #format_ssn = f'{employee_SSN[0:3]}-{employee_SSN[3:5]}-{employee_SSN[5:]}'
    employee_phone = input('Enter employee phone number: ')
    #format_phone = f'({employee_phone[0:3]}) {employee_phone[3:6]}-{employee_phone[6:]}'
    employee_email = input('Enter employee email: ')
    employee_salary = input('Enter employee Salary: ')
    #format_salary = '{:,.2f}'.format(float(employee_salary))
    employee_dash = f'               ----------------------------------------'


    employee_data = {'Employee Name': employee_name, 'SSN': employee_SSN,
                     'Phone Number': employee_phone, 'Email': employee_email, 'Salary': employee_salary,
                     'Dash': employee_dash}

    employee[num_employees + 1] = employee_data
    num_employees = num_employees + 1
    print('New employee added.')
    cls()


# view all employees
def view_all():
    cls()
    print('\nEmployee Details:')
    for emp_id, emp_data, in employee.items():
        print(f'\nEmployee ID: {emp_id}')
        for key, value in emp_data.items():
            if key == 'Employee Name':
                value = f'{"               "}'+ f'{value}'.center(40, '-')
            elif key == 'SSN':
                value = f'SSN: {value[0:3]}-{value[3:5]}-{value[5:]}'
            elif key == 'Phone Number':
                value = f'Phone Number: ({value[0:3]}) {value[3:6]} - {value[6:]}'
            elif key == 'Email':
                value = f'Email: {value}'
            elif key == 'Salary':
                value = 'Salary: ${:,.2f}'.format(float(str(value)))
            elif key == 'Dash':
                value = f'{value}'
            print(f'{value}')
    print(f'There are ({num_employees}) employees in the system.')
    cls()


# edit existing data
def edit_employee():
    cls()
    emp_id = int(input('Enter the ID of the employee you wish to edit.'))
    if emp_id in employee:
        print('Current employee details are:')
        for key, value in employee[emp_id].items():
            print(f'{key}: {value}')

        ###display and choose from numbered list of fields
        print('\nFields:')
        fields = list(employee[emp_id].keys())
        for i, field in enumerate(fields, start=1):
            print(f'{i}. {field}')

        field_choice = int(input('Enter the number corresponding to the field you want to edit: '))
        if 1 <= field_choice <= len(fields):
            field = fields[field_choice - 1]
            new_value = (input(f'Enter the new value for {field}:'))
            employee[emp_id][field] = new_value
            print('Employee information updated.')
        else:
            print('Invalid choice.')
    else:
        print('Employee ID not found.')

#search function
search_SSN = int(input('Please enter the SSN you are searching for: '))

    #############FIX ME###############################################
###I have tried for loops, while true loops, and attempted .get functions, but have had no luck. What i would ultimately like to do if possible is pull the emp_id that i use to edit employee info. Do i need to create a global variable for that? or...






# main menu introduction
def main_menu_intro():
    print('Welcome to the Employee Management System')
    print('This system allows you to add, view, and edit employee information.')
    print('Please select an option from the menu below:')


# main loop for constant running
while True:
    main_menu_intro()
    cls()
    print('Employee Management System')
    print('1. Add employee')
    print('2. View all employees')
    print('3. Edit employee')
    print('4. Search database by SSN')
    print('5. Exit program')
    menu_choice = input('Enter your choice: ')

    if menu_choice == '1':
        add_employee()
    elif menu_choice == '2':
        view_all()
    elif menu_choice == '3':
        edit_employee()
    elif menu_choice == '4':
        search_SSN()
    elif menu_choice == '5':
        print('Exiting program. Goodbye')
        break
    else:
        print('Invalid choice. Please try again.')

0

There are 0 answers