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
Mateusz Konopka
1,212 PointsQuestion about difference between codes
OK so i wrote some code:
def most_courses(my_dict): max_courses = 0 most_talented_teacher = "" for teacher, value in my_dict.items(): if len(value) > max_courses: max_courses = len(value) most_talented_teacher = teacher return teacher
and this is copy of this code:
def most_courses(teacher_dict): max_count = 0 rockstar = "" for teacher, course_list in teacher_dict.items(): if len(course_list) > max_count: max_count = len(course_list) rockstar = teacher return rockstar
And i get different returns
My question is how so? I checked with VS code, python console and i get the same result but in the python challenge i get pass. Can someone pls explain?
Thanks a lot!!
1 Answer
Steven Parker
243,656 PointsIt's hard to read Python code without formatting, and I have to just assume the indentation for both snippets is the same. But other than different variable names, there's one critical difference that makes only the second example correct.
The first example is returning "teacher", which would just be the last one in the list. The one that has the most courses (and the equivalent of "rockstar" in the second example) would be "most_talented_teacher".
Mateusz Konopka
1,212 PointsMateusz Konopka
1,212 PointsI'm sorry i don't know how to paste code in right format..
But i know now what i was doing wrong. I should return most_talented_teacher not teacher.
Thank you for your time buddy!!
Steven Parker
243,656 PointsSteven Parker
243,656 PointsMateusz Konopka — glad to help.
And you can find instructions for code fornatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area.
 Or watch this video on code formatting.