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 trial

Python

teachers.py task 4/5 most_courses question: Code passes but I could use some clarification.

I've recreated this task in my test environment and was able to get it to work. If it is ugly please don't judge me as I'm new to programming lol. My question is; Given the following code, why is "max_val = len(sample_dict[k])" required in order to get the correct key?? Thank you VERY much in advance to anyone that can help build my understanding of this topic.

sample_dict = { 'James': ['Course', 'Table', 'Bird'], 'Kelly': ['Tiller', 'Table'], 'Jill': ['Jumps'], 'Chris': ['Jumps', 'Table', 'Bird'], 'Davis': ['Tiller', 'Hello', 'Brat'] }

print(sample_dict) print('\n')

max_val = 0 teacher = ""

for k in sample_dict.keys(): print(len(sample_dict[k])) if max_val < len(sample_dict[k]): max_val = len(sample_dict[k]) teacher = k

print(max_val) print(teacher)

4 Answers

Hey Christian,

Just for future reference, You can use ```(tick marks above the tab key in the keyboard) before and after your code to make it appear as a code block. It helps to make your code easier to read. :)

In your code, you don't have to iterate over the keys themselves for your code to work, as the for loop is going to do that anyway. You have to put k into sample_dict[k] because you want to reference the value that's being stored there. In this case, the value is an array, and we only care about the length of the array, so we wrap it with len() function.

I just answered another post about this same question, feel free to hop over and look at the code I provided there.

https://teamtreehouse.com/community/need-a-helping-handshove-on-step-45-of-teacherspy

Thank you very much for the explanation and tip regarding the triple quotes. I'm still getting hung up on why I need to store the len of d[k] in a variable in order to get the correct response. I really just chalk it up to my inexperience and will not only look at your other posting but also continue to mess with it in my testing. Thanks again!

I mean, if max_value < len(dict[k])... shouldn't it follow that teacher = k be all that is required?

Oh, I think I understand your question now. You have to store the variable in max_val so the for loop has something for comparison, and you have to store the key's value in teacher so you can return it if it is still the key with the greatest number of courses. The loop will do this:

# First iteration:
max_val = 3
teacher = 'James'

# Second iteration:
max_val = 3 # The loop compared the len(sample_dict[k]) and it kept max_val at 3
teacher = 'James' # It also kept James since the length of the array at k is less than max_val

# More iterations...

Essentially, we need to store a variable outside of the for loop, or the loop will either reset the value to zero(if declared inside of the for loop) or it won't have something to compare to.

I hope that answers your question.

That clears it up completely. I've been in the IT industry for over a decade and always thought that programming wasn't for me but these experiences have been extremely enjoyable. Thanks for helping in clearing that up.