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

Andrew Bickham
Andrew Bickham
1,461 Points

def num_teacher

im only on task one but i don't see why i can clear this first phase???

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.
#
def nun_teachers(string):
   count = dict(len(string.key()))
   return count 

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

You are on the right path. Three items to correct:

  • typo in function name, it's not "nun_teachers"
  • the method of getting the keys is keys, plural
  • creating a dict isn't necessary. The challenge wants the integer result.

Post back if you need more help. Good luck!!

Andrew Bickham
Andrew Bickham
1,461 Points

every detail counts, thank you!

Andrew Bickham
Andrew Bickham
1,461 Points
def num_courses(string):
    number = []
    count = len((string.values())
    number[count] = string.sum(count)
    return number 

all im getting is bummer try again for phase two is there a certain detail im just not seeing?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

You are close again. The code string.values() returns an iterable of the dictionary values. The count should be a sum of each value’s length. A loop like for value in string.values(): would work.

Also, better to name the function parameter something other than β€œstring” since it’s a dict passed in. Try teachers.

Andrew Bickham
Andrew Bickham
1,461 Points
def num_courses(teachers):
    number = []
    for value in teachers.values():
        count = len((teachers.values())
        number[count] = teachers.sum(count)
    return number 

I know im coming to you for every little detail but when I do it on my own workspace, the error I keep getting is a syntax error for "number[count] = teachers.sum(count)", am i writing it incorrectly or is there some other issue?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

The syntax error is coming from the previous line due to missing closing paren.

In the line you mention, the teachers dictionary passed in does not have a sum method. So this too will trigger an error.

The challenge is looking for the total number of courses. This would be the sum of all the count values and not a list of the count values.

Andrew Bickham
Andrew Bickham
1,461 Points

so instead of using the sum method the count method would be the way to go, if im only looking for the total number of courses and not the complete total

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Let's look at the ways you can get information from the teachers dictionary:

  • teachers.values() is a listing of the dictionary values: the course lists
  • teachers.keys() is a listing of the dictionary keys: the teacher's names
  • for value in teachers.values(): len(value) will give you the number of courses for a single teacher
  • add up all of the len(value) to get the total courses
Andrew Bickham
Andrew Bickham
1,461 Points

thank you i was able to get it on to phase three, fair warning they'll probably be some more questions