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 trialshubhamkt
11,675 Points4th challenge
How would I change the argument values to len and get the max from it?
5 Answers
Steve Hunter
57,712 PointsHi there,
You're on the most_courses
method, right?
I created two variables at the top of the method - one to hold the max number of courses currently seen which I initialized to zero. The second was a string to hold the teacher's name, initialised to a blank string.
I then for
looped through the dictionary using the .items()
method. At each iteration I can access the keys and values; I called them teacher
and courses
, I asked if the length of courses was greater than the current max_value
. Since this was initialized to zero, the first iteration would always meet this condition.
If the length of the courses (the values
aspect of the .items()
from the dictionary), was greater, then assign that value into the variable storing the max number of courses and store this teacher's name in the string.
At the end of the loop, return that name.
I hope that helps; I can be more specific if you want but I thought I'd talk you through my thought process to see if that helps you reach a solution.
Steve.
Hasan Jafri
2,859 PointsHi Steve,
When first completing this I did the method you described above but I am still not able to get the correct result from my code. Any recommendations on the changes I should make/consider
def most_courses(dictionary):
most_courses = None
teacher = None
for keys in dictionary.keys():
if len(dictionary[key]) > most_courses:
most_courses = len(dictionary[key])
teacher = key
else:
pass
return teacher
Thanks
Steve Hunter
57,712 PointsHi again!
Three things - unindent your return
as it is inside the loop.
Second, you called the loop variable keys
and you then use key
inside the loop. They need to be called the same.
Lastly, create empty local variables of the correct type; you can't compare an integer to a NoneType
:
most_courses = 0
teacher = ""
That should now pass.
Steve.
Hasan Jafri
2,859 PointsThanks Steve!
I need to keep double checking on what I name my variables
Herman Brummer
6,414 PointsWow, that one way waaaay harder.
def most_courses(dictionary_list):
most_courses = 0
teacher = ""
for keys in dictionary_list.keys():
if len(dictionary_list[keys]) > most_courses:
most_courses = len(dictionary_list[keys])
teacher = keys
else:
pass
return teacher
print most_courses(dictionary_list)
Steve Hunter
57,712 PointsYep - and you can remove your else
block as it isn't doing anything which saves two lines of code.
You can also access the keys and values at the point of the loop which achieves the same thing with, perhaps, a little more clarity/readability:
def most_courses(dict):
max_value = 0
name = ""
for teacher, courses in dict.items():
if len(courses) > max_value:
max_value = len(courses)
name = teacher
return name
Nick Lenzi
7,979 PointsNick Lenzi
7,979 Pointscan you paste in the challenge for us to better help answer your question?