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 trialMUZ140934 Noel Zipingani
2,656 Pointswrite a function named courses that takes the dictionary of teachers.it should return a list of all the course offered
were am getting the last challenge wrong
# 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):
most_class =""
max_count =0
for teacher in dicts:
if len(dicts[teacher])>max_count:
max_count = len(dicts[teacher])
most_class = teacher
return most_class
def num_teachers(test2):
return len(test2)
def stats (test3):
new_list = []
for key in test3:
holder = []
holder.append(key)
holder.append(len(test3[key]))
new_list.append(holder)
return new_list
def courses(teach_dict):
my_list=[]
for teachers in teach_dict():
for courses in teach_dict[teachers]:
my_list.append(courses)
return my_list
2 Answers
MUZ140889 Dephine Chenayi Chihota
6,766 Pointshie Noel, am not getting what you were try to do with your nested for loops but here is how I did it. since the courses are values in the dictionary, I used a for loop to iterate through all the values and added them to my list. NOTE we also want to add these courses as individual items into our list so we use extend.
def courses(teach_dict):
my_list=[]
for course in teach_dict.values():
my_list.extend(course)
return my_list
MUZ140889 Dephine Chenayi Chihota
6,766 Pointsi dont see the problem with my indentation. my code appears to be working just fine, did yours work out.
Boris Ivan Barreto
6,838 PointsI can see in your code that the indentation of the return is at the same level as the second loop for, you should just remove 2 spaces on the indentation in your code and believe me it will work.
I fix your code in my previous answer, so you could compare by yourself.
MUZ140889 Dephine Chenayi Chihota
6,766 Pointsoohh sorry Boris, my bad, thought you were Noel replying to my answer and thanks for the correction. am not getting why he used two for loops though, would you care to explain it to me
Boris Ivan Barreto
6,838 Points@Dephine sorry, my bad too, when I was replying to Noel's question, I didn't realize that you had published an answer, I was just correcting Noel's code, that's it. Then also I thought that Noel was replying to my but it was you.
Your solution is pretty much what I did in my case and of course is much better.
Sean T. Unwin
28,690 Pointsam not getting why he used two for loops though, would you care to explain it to me
He used two loops here because the classes taught by each teacher is a List inside a Dictionary.
Each Key in the Dictionary is the teacher's name and the Value of the Key is the List of classes taught by the teacher.
You could look at this as being two levels deep, therefore the use of two loops.
I hope that helps to make it a little clearer.
The method that you posted originally is definitely more efficient, though. :)
MUZ140889 Dephine Chenayi Chihota
6,766 Pointsthanx but could you explain giving reference to his code so that I understand what he doing in each line of loop. and If try running his code with the indentation corrected its giving me this error ""Bummer! 'dict' object is not callable""
Boris Ivan Barreto
6,838 PointsBoris Ivan Barreto
6,838 PointsHi,
In your code you need to fix the indentation of the return in the courses function.