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

John Schut
John Schut
2,317 Points

Task 2 of teachers.py

Treehouse response with "Bummer! Expected 5, got 18.

But in my seperate workspace I get 5...

Does someone see what goes wrong?

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.

my_dict = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'], 'Kenneth Love': ['Python Basics', 'Python Collections']}

def most_classes(dictonary):
  max_count = 0
  teacher_most_classes = ''
  for item in dictonary:
    if len(dictonary[item]) > max_count:
      max_count = len(dictonary[item])
      teacher_most_classes = item
  return(teacher_most_classes)

most_classes(my_dict)

def num_teachers(dictonary2):
  teachers_total = 0
  for item in dictonary2:
    teachers_total += len(dictonary2[item])
  return(teachers_total)

num_teachers(my_dict)

5 Answers

Steven Parker
Steven Parker
230,995 Points

For the challenge, you don't supply the data and don't call your function (only define it). The challenge handles the rest.

Also, don't assume that the challenge is going to test your function with the exact same data shown in the commented sample. The sample only has 2 teachers and 5 classes, but from the error message you got we can infer that the testing is being done with data having 5 teachers and 18 classes.

The actual issue is that your num_teachers function is adding the number of classes (len(dictonary2[item])) to the teachers_total. But you only need to add 1 for each teacher (teachers_total += 1).

You actually would only need to return the length of the dictionary2 parameter:

def num_teachers(dictonary2):
  return len(dictonary2)
Rakshit H Ravishankar
Rakshit H Ravishankar
1,309 Points

the second task is quite simple, here is my code

def num_courses(dict1): count=0 for i in dict1: list1=dict1[i] for i in list1: count+=1 return count

Anders Axelsen
Anders Axelsen
3,471 Points

Thank you Rakshit, this made sense to me.

Fenimore Cooper
Fenimore Cooper
3,591 Points

Hi John,

You might want to double check the question. The second task is actually to find the number of teachers, as Iain indicates... you've counted the total number of classes taught, which is actually fulfilled something closer to the fourth task. So congrats for being ahead of things.

Steven is also correct -- that the sample dictionary (with two teachers and five courses) is not the same dictionary that they text your function against. This is the ultimate source of your confusion. The sample dictionary is only there so that you can understand the structure of the data. The testing dictionary or dictionaries are unknown to you -- and you might only see parts of them if you get an error.

At times, it would probably be useful to have more transparency in testing routines, or in their output -- but that's not the way things are. If your function returns an output of 2 with the sample dictionary, it should also return a 5 with their test dictionary -- and then you should be golden.

FC

John Schut
John Schut
2,317 Points

Hi Iain, then I get 2. This is indeed the number of teachers.

BUT, the second task was to count the number of courses (in my opinion). I agree that the name num_teachers makes you think otherwise, but the correct answer according to Treehouse is 5 (which is indeed the number of courses).

My 'problem' is that according to Treehouse my answer is 18 (although in my own workspace the answer is 5... :-( ).

I solved the task in 'an other way'. Thanks for your response.

John Schut
John Schut
2,317 Points

Hi Fenimore, thanks for clearing that up!