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

Owen Grace
Owen Grace
5,292 Points

Values in dictionary are ints?

Hi There,

When trying to create the stats function, the console returns the error "object of type int has no len()"

Code:

def stats(dicts): for i in dicts: dicts[i] = len(dicts[i]) return dicts

This code functions perfectly in the workspace and returns a dictionary with the teachers and the quantity of classes they teach. Any ideas why this doesnt work in this exercise?

Thanks

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.
def most_classes(dicts):
  winner = ""
  max_count = 0
  for i in dicts:
    if winner == "":
      max_count = len(dicts[i])
      winner = i
    elif len(dicts[i]) > max_count:
      max_count = len(dicts[i])
      winner = i
  return winner

def num_teachers(dicts):
  total = 0
  for i in dicts:
    total += 1
  return total

def stats(dicts):
  for i in dicts:
    dicts[i] = len(dicts[i])
  return dicts

2 Answers

Brandon Wall
Brandon Wall
5,512 Points

Try this code.

def stats(adict):
  statlist = []
  for key, value in adict.items():
    sub_list = [key, int(len(value))]
    statlist.append(sub_list)
  return statlist

My only hypothesis for you getting that error was that somehow in the course of the challenge you somehow managed to modify the values of the dictionary. I just got that error in my python interpreter after using a for loop to change each key's value to an integer, than trying to run the stat function again. I can't yet find a reason why your solution shouldn't work unless the values of the dictionary were modified. :\ SMH

Scratch all that, i figured it out! Your version of the stats function changes the values assigned to the keys of the dictionary to integers, wiping out the original dictionary, that is why it's giving you an error.

Trying to run your stats function twice in a row results in an error on the second time because all the values assigned to the keys of the dictionary have been change to integers. Use a function that preserves the natural order of the dictionary while giving the length of the number of classes.

Owen Grace
Owen Grace
5,292 Points

Hi There - thanks for the response, definitely helped.

I think the simple answer is that the question is asking for a "list of lists" and I was returning a dictionary. Yours returns a list, so works.

Thanks for you help again - I need to read the question better!