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

Andrew Bickham
PRO
Andrew Bickham
Pro Student 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 56,839 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
Pro Student 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 56,839 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
PRO
Andrew Bickham
Pro Student 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 56,839 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
Pro Student 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 56,839 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
Pro Student 1,461 Points

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