Jay C.
Jay C.

Reputation: 53

How to search for a nested list by user input of a particular element

Newbie learner here. I have a issue where I'm trying to implement new features into code and getting hung up on how to implement the last set of features. I'm trying to allow the user to search via one of the attributes - SSN - and pull the associated nested list with that SSN. I'm trying to set the search as a def()function so I can put it in a line of if/elif branches at the latter end of the code. Any help is much appreciated.

Regards,

Web searches, course text, python docs, other forum posts.

# You have to use function arguments and returns. Check my code and you can modify it.

def add_emp(emp_list, num):
    for i in range(num):  # number of employee variable inputs
        employee = []
        for attribute in ['Name', 'SSN', 'Phone', 'Email', 'Salary']:
            employee.append(str(input(f'{attribute}: ')))
        print(f'Employee {i} : ', ' '.join(employee).title())  # print employee information
        emp_list.append(employee)
    # and when you finish, all employee are here



# Next, user request list info
def choose_list(emp_list): 
    print("You can pull an individual's list by entering 1 - 5")
    query_emp_list = int(input("Choose 1 - 5: ")) -1
    if 0 <= query_emp_list < len(emp_list):
        print(' '.join(emp_list[query_emp_list]))


# section for new format I'm having issue with
def new_emp_format(emp_list):
    for employee in emp_list:
        [Name, SSN, Phone, Email, Salary] = employee
        print(f"---------------------------- {Name} -----------------------------")
        print(f'SSN: {SSN}')
        print(f'Phone: {Phone}')
        print(f'Email: {Email}')
        print(f'Salary: {Salary}')


def main():
    # user instructions to begin creating list
    print('Please enter your Employee information in the below prompts')

    emp_list = []
    num_emp = int(input("How many Employee's do you want to add? "))

    add_emp(emp_list, num_emp)
    choose_list(emp_list)


    # Beginning of While loop
    while(True):
        if(input("Do you need another list? (y/n): ")=='y'): # retry request loop
            choose_list(emp_list)
            continue
        elif(input("Want to know how many employees there are? (y/n): ")=='y'):
            print('There are', len(emp_list), 'employees in the system.')
            continue
        elif(input("Would you like to add another Employee? (y/n): ") =='y'):
            add_emp(emp_list, 1)
            continue
        elif(input("Would you like to view all lists in new format? (y/n): ") =='y'):
            new_emp_format(emp_list) # output current nested lists in new format
            continue
        elif(input("Would you like to search employee by SSN? (y/n): ") =='y'): 
            #section to add in new search via SSN
            print('')
            continue
        else:
            break


if __name__== "__main__":
    main()

Expected results should look like this after user query of either all the lists or a single nested list

---------------------------- Mike Smith -----------------------------

SSN: 123123123

Phone: 111-222-3333

Email: mike@g'mail.com

Salary: $6000


current output is the base list structure: [Mike Smith, 123123123, 111-222-3333, mike@g'mail.com, $6000]

Upvotes: 1

Views: 465

Answers (1)

Lamanus
Lamanus

Reputation: 13541

You have to use function arguments and returns. Check my code and you can modify it.

def add_emp(emp_list, num):
    for i in range(num):  # number of employee variable inputs
        employee = []
        for attribute in ['Name', 'SSN', 'Phone', 'Email', 'Salary']:
            employee.append(str(input(f'{attribute}: ')))
        print(f'Employee {i} : ', ' '.join(employee).title())  # print employee information
        emp_list.append(employee)
    # and when you finish, all employee are here
    print(emp_list)
    return emp_list


# for additional employees
#def add_extra_emp():
#    for i in range(1):  # number of employee variable inputs
#        employee = []
#        for attribute in ['Name', 'SSN', 'Phone', 'Email', 'Salary']:
#            employee.append(str(input(f'{attribute}: ')))
#        print(f'Employee {i} : ', ' '.join(employee).title())  # print employee information
#        emp_list.append(employee)
# Function is duplicated with add_amp, so it doesn't needed        


# Next, user request list info
def choose_list(emp_list): 
    print("You can pull an individual's list by entering 1 - 5")
    query_emp_list = int(input("Choose 1 - 5: ")) -1
    if 0 <= query_emp_list < len(emp_list):
        print(' '.join(emp_list[query_emp_list]))


# section for new format I'm having issue with
def new_emp_format(emp_list):
    for employee in emp_list:
        [Name, SSN, Phone, Email, Salary] = employee
        print(f"---------------------------- {Name} -----------------------------")
        print(f'SSN: {SSN}')
        print(f'Phone: {Phone}')
        print(f'Email: {Email}')
        print(f'Salary: {Salary}')


def main():
    # user instructions to begin creating list
    print('Please enter your Employee information in the below prompts')

    emp_list = []
    num_emp = int(input("How many Employee you want to add?"))

    add_emp(emp_list, num_emp)
    choose_list(emp_list)


    # Beginning of While loop
    while(True):
        if(input("Do you need another list? (y/n): ")=='y'): # retry request loop
            choose_list(emp_list)
            continue
        elif(input("Want to know how many employees there are? (y/n): ")=='y'):
            print('There are', len(emp_list), 'employees in the system.')
            continue
        elif(input("Would you like to add another Employee? (y/n): ") =='y'):
            add_extra_emp(emp_list, 1)
            continue
        elif(input("Would you like to view all lists? (y/n): ") =='y'):
            new_emp_format(emp_list) # output current nested lists in new format
            continue
        elif(input("Would you like to search employee by SSN? (y/n): ") =='y'): 
            #section to add in new search via SSN
            print('')
            continue
        else:
            break


if __name__== "__main__":
    main()

Upvotes: 1

Related Questions