Tarzan
Tarzan

Reputation: 124

Python function looping dictionaries

I'm trying to output employee information in a specific format.

dep_tup is a tuple with all the different department names the user input (no duplicates).

dep_dict is a dictionary with the departments as the key and the list full of employee dictionaries as the value associated with the key.

#Function that outputs the employees by department
def deps():
    for department in dep_tup:
        i = 0
        total = len(dep_dict[department])
        for i in range(len(dep_dict[department])):
            return "%s with %d employees\n" % (dep_dict[department], total)
            return "\n%s: %s, %s, $%f per year\n" % (dep_dict[department][i]["name"], dep_dict[department][i]["position"], dep_dict[department][i]["em_department"], dep_dict[department][i]["salary"])
            i += 1

For example, if the user inputs 1 employees from the sales department and 1 employee from operations, it should output something like this:

#Sales with 1 employees
#John Doe: sales lead, sales, $50000.00 per year
#Operations with 1 employees
#Jane Doe: technician, operations, $60000.00 per year

Upvotes: 0

Views: 39

Answers (1)

0TTT0
0TTT0

Reputation: 1322

I think you just want to print, once you return it returns what is to be returned and ends the function. Also you do not need to set i and increment it, as the for loop automatically does that. The following should work (but i cannot be sure without being provided the initial input)

def deps():
    for department in dep_tup:
        total = len(dep_dict[department])
        for i in range(len(dep_dict[department])):
            print("%s with %d employees\n" % (dep_dict[department], total))
            print("\n%s: %s, %s, $%f per year\n" % (dep_dict[department][i]["name"], dep_dict[department][i]["position"], dep_dict[department][i]["em_department"], dep_dict[department][i]["salary"])

Upvotes: 1

Related Questions