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 trialMUZ140889 Dephine Chenayi Chihota
6,766 Pointsteacher stats
why is my code returning only 5 courses
# 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(my_dict):
max_count=0
busy_teacher=''
for x,y in my_dict.items():
if len(y)>max_count:
max_count=len(y)
busy_teacher=x
return busy_teacher
def num_teachers(my_dict):
return len(my_dict)
def stats(my_dict):
teacher_list=[]
for key in my_dict:
my_list=[]
my_list.append(key)
my_list.append(len(my_dict[key]))
teacher_list.append(my_list)
return teacher_list
def courses(dict):
courses_list=[]
vals = dict.values()
for course in vals:
courses_list.append(course)
return courses_list
1 Answer
Matthew Rigdon
8,223 PointsThe dictionaries that are going into your code look like this:
my_dict = {{'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'], 'Kenneth Love': ['Python Basics', 'Python Collections']}
Your course portion of your code looks like this:
def courses(dict):
courses_list=[]
vals = dict.values()
for course in vals:
courses_list.append(course)
return courses_list
In your For loop, Python is looking at the values as a list (['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations']), then appending that entire group onto the end of course_list. Next, it appends (['Python Basics', 'Python Collections']) all as one list onto courses_list. If I wanted to know the len(courses_list) I would get 2. That is because there are two lists, however, you have lists inside of those lists that are not being accounted individually.
I solved this issue by making a For loop for each of the keys (teachers names), and then another For loop to go through and append each values (classes) separately. If you need more assistance writing that code, let me know. Also, this website allows you to visualize what is happening in Python code, so you may want to check it out: www.pythontutor.com