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 Python Collections (Retired) Dictionaries Teacher Stats

What's the wrong with my code > "most_classes should return the teacher with the most classes"

Each and every time I run the code ,it gives me different values of a and b.Why does it occur? Its okay that list doesn't follow the order in iteration.For any number of execution through the loops ,the value of max should be the highest.But why does it change,even show different output for the values of a and b !

teachers.py
# 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.
ite={'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
  'Kenneth Love': ['Python Basics', 'Python Collections'],'Jason Bourne':['black berry','mango','potato','oli','key'],
     'fruits':['cherry','pineapple','banana','coconut','apple','lemon']}

def most_classes (ite):
    c = 0
    for i in ite:
        max=len(ite[i])
        #print(i)
        #print (c)
        #print (max)
        #print(" ")
        #print(c)
        if max>c:
            a=len(ite[i])
            b=i
            #print(b)
            #print (a)

            c = a
            #print (c)
        print (b)
        print(a)
        c=a
        #print (c)

most_classes(ite)

1 Answer

Ryan S
Ryan S
27,276 Points

Hi Kawser,

I'm not quite sure if I understand what you are asking, but the logic of your code will pass the challenge (assuming you return "b" instead of print, and be careful with the indentation of the return statement).

Dictionaries are unordered so every time you execute the code the order of the iteration may be different. Your values of "a" and "b" will change because each loop iteration will update the variables as it finds another dictionary value with a greater length. Since you are printing the variables in each loop iteration, you will see how they are being updated.

So in your ite dictionary, if "fruits" is the first key that is iterated through, those values of "a" and "b" won't change for the remaining loops because they will already have found the max value. If "fruits" is the last iteration, then your variables will change on the loops leading up to that.

Hope this helps.