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 trialGabriel McGraw
2,317 PointsCode is not passing correctly in treehouse, I've tested it in an IDE and it works!
The function that's not passing is "def most_courses(my_dict):".
# The dictionary will look something like:
my_dict = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics','example1','example2'],
'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(my_dict):
lst = []
for k,v in my_dict.items():
lst.append(k)
return len(lst)
def num_courses(my_dict):
lst = []
for k,v in my_dict.items():
for i in v:
lst.append(i)lst = []
for k,v in my_dict.items():
return max(numcourses(my_dict))
return len(lst)
def courses(my_dict):
lst = []
for k,v in my_dict.items():
for i in v:
lst.append(i)
return lst
def most_courses(my_dict):
lst = []
for k,v in my_dict.items():
lst.append(len(v))
most = max(lst)
for k,v in my_dict.items():
if len(v) == most:
return k
4 Answers
leonardbode
Courses Plus Student 4,011 PointsHey Gabriel, try this:
def most_courses(x):
teacher, max_count = "", 0
for key, value in x.items():
count = 0
for i in value:
count += 1
if count > max_count:
teacher, max_count = key, count
return teacher
If you have any questions, I will edit my answer accordingly. If you do not have any questions, remember to upvote and choose best answer so that this question receives a checkmark in forums.
Gabriel McGraw
2,317 PointsIt just says "Bummer, try again" when I run it, there's no actual error.
leonardbode
Courses Plus Student 4,011 PointsI assume you uncommented my_dict?
Recomment it by putting # on lines 2 and 3:
# The dictionary will look something like:
# my_dict = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics','example1','example2'],
# '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.
Gabriel McGraw
2,317 PointsOkay I figured it out, when I clicked next on the previous steps it changed my code. Thanks for your help though.
William Warren
2,519 Pointsi'm having the same issue
any ideas?
'''python def most_courses(t_dict): max_teach = " " max = 0 count = 0 info = t_dict.values() names = t_dict.keys() for i in info: if max < len(i): max = len(i) max_teach = names[count] count += 1 else: count += 1 return max_teach '''
William Warren
2,519 PointsI'm getting a Type Error which reads "Dict_keys" object does not support indexing, yet it works in an online code fiddle.
leonardbode
Courses Plus Student 4,011 PointsWilliam Warren Your code isn't formatted correctly, could you change it please and I'll take a look at it?
Gabriel McGraw
2,317 PointsGabriel McGraw
2,317 PointsIt's not passing through either. Your solution does work in an IDE though.
leonardbode
Courses Plus Student 4,011 Pointsleonardbode
Courses Plus Student 4,011 PointsCould you elaborate, please? What is the error message displaying.
Replace your code with this code:
Remember to upvote and choose best answer so that your question receives a checkmark in forums.