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 trialNitin Kakkar
5,223 PointsI am getting strange errors on the site but in workflow code work fine. Can you share dict that is used to test our code
My code is working both on my side and in workspace provided on teamtreehouse. However when I put in my answer, code tester is throwing some strange errors. Can you please share dict passed to test this code and I am attaching my code, I will really appreciate if you can indicate what am I doing wrong?
def stats(my_dicts): listoflists = [] list =[]
for dict in my_dicts: list = [] for key in dict: list.append(key) # list.append(len(dict[key])) listoflists.append(list)
return(listoflists)
# 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_dicts):
max_course = int(0)
max_course_teacher = "Jason Seifer"
for dict in my_dicts:
for key in dict:
if (int(len(dict[key])) > max_course):
max_course = int(len(dict[key]))
max_course_teacher = key
return max_course_teacher
def num_teachers(my_dicts):
total_teachers = int(0)
for dict in my_dicts:
total_teachers += 1
return(total_teachers)
def stats(my_dicts):
listoflists = []
list =[]
for dict in my_dicts:
list = []
for key in dict:
list.append(key)
list.append(int(len(dict[key])))
listoflists.append(list)
return(listoflists)
2 Answers
Kenneth Love
Treehouse Guest TeacherThis is how I typically solve this:
def most_classes(teachers):
# Set up variables for remembering high counts
high_count = 0
busy_teacher = ''
for teacher in teachers: # Look at each teacher's dict in turn
if len(teachers[teacher]) > high_count: # If the value for that teacher's key is high
high_count = len(teachers[teacher]) # update the count
busy_teacher = teacher # update the teacher
return busy_teacher # Once we're through them all, return the busiest
Kenneth Love
Treehouse Guest TeacherI'm digging through your code right now. Here's what I'm seeing:
- It's not a great idea to use variable names that are the same as types. I'd recommend you rename
list
,dict
, etc in your code. - On
most_classes
, in yourfor dict in my_dicts
,dict
is a teacher's name (a key in the dictionarymy_dicts
). When you then loop through that, you're just looping through the letters. Thatlen()
is going to be 1 every single time. - In
num_teachers
, you can just dolen(my_dicts)
.
Nitin Kakkar
5,223 PointsThanks a lot Kenneth. I really enjoy your course. Thanks for feedback it help clear things a lot. I have one more question I modified most_classes as follows
my_dicts = [
{'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations']},
{'Kenneth Love': ['Python Basics', 'Python Collections']}
]
def most_classes(my_dicts):
max_course = int(0)
max_course_teacher = ''
for one_dict in my_dicts:
teacher = one_dict.keys()
courses = one_dict.values()
print(len(courses))
With len(courses) I was hoping to total number of courses for each teacher. However it only gives me 1. How can i get total number of courses by a teacher ?
I could do following to get total number of courses but I dont this that is correct way
def most_classes(my_dicts):
max_course = int(0)
max_course_teacher = ''
for one_dict in my_dicts:
teacher = one_dict.keys()
courses = one_dict.values()
for course in courses: # this gives correct length why ?
print(len(course))