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 (2016, retired 2019) Dictionaries Teacher Stats

Damian McCarthy
Damian McCarthy
3,656 Points

I don't get why none of the things I try wont pass.

I really am not understanding how to iterate through the values in the dict.

teachers.py
# The dictionary will look something like:
# {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Each key will be a Teacher and the value will be a list of courses.
#
# Your code goes below here.
teachers = {'Andrew Chalkley' : ['jQuery Basics', 'Node.js Basics'],
            'Kenneth Love' : ['Python Basics', 'Python Collections']}
def num_teachers(teachers):
    count = 0
    for amount in teachers.keys():
        count += 1

    return count



def num_courses(teachers):
    count = 0
    for amount in teachers.value():
        for value in teacher[amount]:
        count += 1

    return count

1 Answer

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

A couple things:

  • You want the method named values not value on the teachers dictionary.
  • looking at the sample data, each of those values is a list. You're calling that variable amount, but you don't have an amount yet, you just have a list. You need to get the length of the list. Here is my solution:
def num_courses(teachers):
    count = 0
    for courses in teachers.values():
        count += len(courses)
    return count

The treehouse challenges don't give you very good error messages. ("Bummer! Try again!"). Sometimes it's a good idea to take the problem and do it in an environment where you see the specific detailed error messages from Python, or you can print out variables to the console and debug, whether that's a Treehouse workspace or your own text editor and terminal.