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

That one wasn't too bad, right? Let's try something a bit more challenging. Create a new function named num_courses tha

hoe do i do it

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.
def num_teachers(techers):
    count = 0
    for teacher in techers:
        count += 1
    return count

def num_courses():

4 Answers

Alexandra Barnett
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexandra Barnett
Front End Web Development Techdegree Graduate 46,473 Points

Hi Jalil! I can talk you through it, step by step:

  • You have the function already defined, but it needs an argument just like the previous stage.
  • As the challenge wants to know the total number of courses, there could be a variable to hold that total e.g. total = 0
  • The courses are in the value "part" of the key, value pair in the dictionary so, you could do a for loop to get the values e.g. for key, value in a_dict.items()
  • In the for loop, you could then find the length of the value and add it to the total variable you made at the start.
  • Finally, you can then return the total

I hope that helps - I put it in words for you, so you could see if you can code it by yourself but, if you get stuck just let me know and I can show you what I did :)

ok

help i don't get it

Alexandra Barnett
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexandra Barnett
Front End Web Development Techdegree Graduate 46,473 Points
def num_teachers(a_dict):
    return len(a_dict)

# define the function taking in a dictionary as the argument
def num_courses(a_dict):
    # make a total variable to store the total number of teachers that will need to be returned at the end of the function
    total = 0
    # loop over the dictionary
    for key, value in a_dict.items():
        # for each entry in the dictionary, find the length (number of courses) of the value (a list of courses) and add to total
        total += len(value)
    # finally, return the total
    return total

Hopefully this helps to explain what is going on :)

ok thanks that one was rely hard

thanks

my proble was i dent folow the for key, value in a_dict.items():

i dinkt that was a e.g