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

Problem with Task 4: Bummer! You returned 5 courses, you should have returned 18 EDIT!: Solved, but still a question.

I have no idea where the 18 classes are coming from when the list only has 5. I am guessing that its an issue with the challenge not wanting an input, but I have no idea how to get around having an input to pass to the function. I seem to be having this problem a lot, and usually I can figure it out, but I am stumped on this one

The error I am getting is Bummer! You returned 5 courses, you should have returned 18

NOTE: The prints are there so I can check my work was I go

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.
dict ={'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'], 'Kenneth Love': ['Python Basics', 'Python Collections']}
def most_classes(dict):
    max_classes = 0
    for entry in dict:
        if len(dict[entry]) > max_classes:
            max_classes = len(dict[entry])
            max_teacher = entry
    return (max_teacher)

#Count the number of teachers in the dictionary
def num_teachers(dict):
    teacher_qty = 0
    for entry in dict:
        teacher_qty += 1
    return teacher_qty

#Now, create a function named stats that takes the teacher dictionary. Return a list of lists in the format [<teacher name>, <number of classes>]. 
#For example, one item in the list would be ['Dave McFarland', 1].
def stats(dict):
    teacher_stats = []
    for entry in dict:
        teacher_stats.append([entry,len(dict[entry])])
    return teacher_stats

#Great work! Finally, write a function named courses that takes the teachers dictionary. 
#It should return a single list of all of the courses offered by all of the teachers.
def courses(dict):
    class_list = []
    for entry in dict:
        class_list.append(", ".join(dict[entry]))
    return class_list

print(most_classes(dict))
print(num_teachers(dict))
print(stats(dict))
print(courses(dict))

EDIT:

I figured out my problem. When I was pulling the items out of the list with the first join it was giving me 'a,b,c' instead of 'a','b','c'.

def courses(dict):
    class_list = []
    for entry in dict:
        class_list.append(", ".join(dict[entry]))
    class_list = ",".join(class_list)
    class_list = class_list.split(',')
    return class_list

Was there any nicer way to do this? I felt like at first they wanted a format function call, but I couldn't find a nice way to do it without rewriting the dictionary to read {'classes':'a','b','c' , 'classes':'d','e','f'}

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

The easiest way is to extend (docs) instead of append:

def courses(dict):
    class_list = []
    for entry in dict:
        class_list.extend(dict[entry])
    return class_list