Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Dictionaries Teacher Stats

What is the code?

What is the code?

teachers.py
def num_teachers(test2):
    return len(test2)
def num_courses(a_dict):
    a_list = []
    for key in a_dict:
    for item in key:
        if item not in a_list:
        a_list.append(item)
        else:
        continue
    return a_list

3 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,258 Points

your indentation is off. the second for loop has to be indented a tab to be inside the first for loop. also the body of the if statement must be a tab in from the if. the else goes at the same level as the if but the rest goes one tab in (right).

"PLEASE PROVIDE SOLUTION FOR THIS"Wow, I just can't stump you! OK, two more to go. I think this one's my favorite, though. Create a function named most_courses that takes our good ol' teacher dictionary. most_courses should return the name of the teacher with the most courses. You might need to hold onto some sort of max count variable.

Joshua Dam
Joshua Dam
7,148 Points

You need to dig into the dictionary a little deeper. All you're pulling out of the dictionary are the lists. You need to get the values inside of the lists. You also don't need to include else: continue. The FOR statement will only run a certain number of times

Also, upon further inspection: using "for item in key:" is pulling out every character from just the keys. So it outputs this: ['A', 'n', 'd', 'r', 'e', 'w', ' ', 'C', 'h', 'a', 'l', 'k', 'y', 'K', 't', 'L', 'o', 'v']

Therefore, you need to use "for key, value" as shown below

for key, value in a_dict.items():
      for value in value:
krishnaprasad RS
PLUS
krishnaprasad RS
Courses Plus Student 413 Points
def most_courses(teacher_dict):
    for name, course in teacher_dict.items():
        if max(course):
            return name