Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (Retired) Dictionaries Teacher Stats

I don't get how I'm wrong (Code Challenge)

I'm on part 3 of the Code Challenge. When I enter my code as it is below, it says that "'str' object has no attribute 'append'".

I'm pretty sure it's pointing to the third to last line. However, I don't think item is a string, because I made sure that it was a list, as you can see in the last function, stats. How can I get the correct answer?

teachers.py
# The dictionary will be something like:
# {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Often, it's a good idea to hold onto a max_count variable.
# Update it when you find a teacher with more classes than
# the current count. Better hold onto the teacher name somewhere
# too!
#
# Your code goes below here.
def most_classes(dict):
    max_count = 0
    max_teacher = None
    for key in dict:
        count = 0
        value = dict[key]
        for item in value:
            count += 1
            if count > max_count:
                max_count = count
                max_teacher = key
    return max_teacher
def num_teachers(dict):
    teacher_count = 0
    for key in dict:
        teacher_count += 1
    return teacher_count
def stats(dict):
    dict_list = []
    for key in dict:
        count = 0
        item = []
        item.append(key)
        for item in dict[key]:
            count += 1
        item.append(count)
        dict_list.append(item)
    return dict_list

you cant append count because its not an iterable your just increasing the value of count and still storing it in the count variable name.

def stats(teacher_dict):
    teachers  = []
    total = []
    result = []
    for k,v in teacher_dict.items():
        teachers.append(k)
        total.append(len(v))
    for t in teachers:
        result.append([t,total[teachers.index(t)]])
    return result

Oh okay. Thanks!

1 Answer

Steven Parker
Steven Parker
229,644 Points

You've already declared and assigned item when you get to this line:

        for item in dict[key]:

At that point it gets re-used to iterate through the strings of class names, ending up as one of the strings. But then you do this:

        item.append(count)

And that causes the error.

:point_right: You might want to pick a different variable name to iterate through the list of classes.

Thank you so much! I didn't realize that.