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 trialMohammed Ismail
7,190 Pointsmost_courses
Hi,
Can someone help me in solving this code:
def most_courses(teachers_dict):
most_course= 0
for key in teachers_dict.keys():
for value in teachers_dict.values():
if len(value)> most_course:
most_course=len(value)
teacher = str(key)
return teacher
#teachers_dict = {"Kenneth": ["Happy","Rocky"],"James": ["Happy1","Rocky1","Dotnet"]}
#print(most_courses(teachers_dict))
teachers.py
# The dictionary will look something like:
# {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
# '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(teachers_dict):
count = 0
for x in teachers_dict.keys():
count+=1
return count
def num_courses(teachers_dict):
count_c=[]
for courses_c in teachers_dict.values():
for x in courses_c:
count_c.append(x)
return len(count_c)
def courses(teachers_dict):
count_c=[]
for courses_c in teachers_dict.values():
for x in courses_c:
count_c.append(x)
return count_c
def most_courses(teachers_dict):
most_course= 0
for key in teachers_dict.keys():
for value in teachers_dict.values():
if len(value)> most_course:
most_course=len(value)
teacher = str(key)
return teacher
#teachers_dict = {"Kenneth": ["Happy","Rocky"],"James": ["Happy1","Rocky1","Dotnet"]}
#print(most_courses(teachers_dict))
2 Answers
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsHey Mohammed Ismail
def most_courses(teachers_dict):
most_course= 0
for key in teachers_dict.keys(): # This line
for value in teachers_dict.values(): # and this line
if len(value)> most_course:
most_course=len(value)
teacher = str(key)
return teacher
So it looks like you are running two loops that loop over the same dict. Except in one you are looping over each KEY, then the other you are looping over each VALUE. But because they are nested loops. You are basically duplicating yourself in this loop.
You are saying " For each KEY(teacher), I want to loop over EVERY teachers set of values" so you end up looping over the same data multiple times. You should be able to loop over the keys and values at the same time and complete this using only 1 loop. :)
Check out this area of the Python docs for Looping Techniques with Dictionaries.
Hint: Is the first example in that link.
Mohammed Ismail
7,190 PointsThanks Chris!
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsNo problem, hope it helped!
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsChris Howell
Python Web Development Techdegree Graduate 49,702 PointsFixed: Markdown code formatting