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 trialDmitri Weissman
4,933 Pointscode working on my computer, but can not submit task
the code in teachers.py is working perfectly well on my computer though when i press check work, I get the "Try again!" message the function that was added is most_courses
# 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(dic):
return len(dic)
def num_courses(dic):
l = 0
for t in dic:
l += len(dic[t])
return l
def courses(dic):
l = list()
for t in dic:
l.extend(dic[t])
return l
def most_courses(dic):
for t in dic:
dic[t] = len(dic[t])
return (list(dic.keys())[list(dic.values()).index(sorted(list(dic.values()))[-1])])
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsYou've asked a good question!
Try running your code twice:
# define a dictionary
courses = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics', 'B', 'C'],
'Kenneth Love': ['Python Basics', 'A', 'Python Collections']}
# first run
print(most_courses(courses))
# second run
print(most_courses(courses))
# this yields the results
Andrew Chalkley
Traceback (most recent call last):
File "<string>", line 33, in <module>
File "<string>", line 25, in most_courses
TypeError: object of type 'int' has no len()
Explanation: Python passes function arguments using reference pointers. When the code
dic[t] = len(dic[t])
is run during the first execution, the original dict courses
is altered by replacing the list of courses with an integer.
During the second run, the dict values are now all integers.
When working with dictionaries as parameters, it is important not to alter them unless, in the unusual case, it is intentional.
Post back if you need more help. Good luck!!!
Dmitri Weissman
4,933 PointsThank you very much for the detailed explanation. Now when I know that it can run few time in background during check, it's perfectly clear. I was assuming that it will run only once and intentionally altered the dictionary due to laziness to create and handle another variable.
I'm wondering if you can help me to understand another issue: here it the code for the first 3 steps:
def num_teachers(dic):
return len(dic)
def num_courses(dic):
l = 0
for t in dic:
l += len(dic[t])
return l
def courses(dic):
t = list()
for t in dic:
t.extend(dic[t])
return t
while passing on it's own, it was not important what I tried to use later would fail. And I had quite a few versions of the most_courses(). Replacing the courses() function with another version that does it in a slightly different way, it was was solved. here is a sample of the courses() that didn't prevent the code to work:
def courses(dic):
t = []
for i in dic.values():
t.extend(i)
return t
any idea what went wrong ?
Dmitri Weissman
4,933 PointsDmitri Weissman
4,933 Pointsnow it gives the TypeError: object of type 'int' has no len() :/