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

Ronnie Barua
Ronnie Barua
17,665 Points

Need help with Key and Value, also to compare them to find the most classes.

I've tried with vain but failed, now it's your turn. Please be kind enough to explain why. Here is what I have. Thanks in advance!

def most_classes(x):
max_count = {}
busy_teacher = 0

for keys, values in x:
if len(value) >= max_count:
len(value) = max_count
busy_teacher = key
return busy_teacher

19 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

I added formatting strings to your post. It looks like your code isn't indented at all. Is that accurate?

Ronnie Barua
Ronnie Barua
17,665 Points

You are right. I have problem with indentation, thought it's 4 spaces from the left. I am wrong but I am glad that I was able to write the code.

Kenneth you are the best teacher. I appreciate all your help. Thanks!

Ronnie Barua
Ronnie Barua
17,665 Points

busy_teacher = key. This is where I have indented 6 spaces just don't know which way to go right or left. Tried 4 spaces that doesn't hold. Very sorry about this.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Every step of indentation has to be the same. Python doesn't really care how much it is, just so long as it's consistent. The style guide recommends 4, though.

def most_classes(x):
    max_count = {}
    busy_teacher = 0

    for keys, values in x:
        if len(value) >= max_count:
            len(value) = max_count

Notice how each block's contents are indented 4 spaces more?

Ronnie Barua
Ronnie Barua
17,665 Points

You are just great. I'll try that right now. Thanks again.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Just to clarify, I'm not saying the above code is correct. Just showing you how to properly format it.

Good luck!

Ronnie Barua
Ronnie Barua
17,665 Points

Alright, I need to rewrite my code. Now what is correct then?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

A lot of your code is good. Look at the len(value) = line (you'd want to assign len(value) to something, not something to len(value). And you probably don't want max_count to a dictionary or busy_teacher to be an integer.

Ronnie Barua
Ronnie Barua
17,665 Points

What would be the right step in this case. I mean how do I go about it?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Which part are you stuck on? The above comment should fix the one actual problem with your code (the assignment part). How do you assign a variable?

Ronnie Barua
Ronnie Barua
17,665 Points

I have trouble with if statement because can't compare those values and can't find any good variables that can be easily converted to int.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Why is your max_count a dictionary? It's going to be holding a number, right? So shouldn't it be an int?

Why is busy_teacher an integer? It's going to hold a string (the teacher's name), so shouldn't it start as an empty string?

Ronnie Barua
Ronnie Barua
17,665 Points

Yes, I've been thinking and tried few ways without any success.

Yes, but how do I do that.

Do I even need a loop or a if statement?

Thanks Kenneth.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Yes, you'll need both a for loop and an if.

Let's break it down step-by-step.

  1. Start a function named most_classes. It'll take an argument that will be a dictionary of teachers.
  2. Set up a variable for holding the current highest number of classes. Right now this should be 0 because you've seen no classes.
  3. Set up a variable for holding the teacher with the highest number of classes. Right now this should be an empty string because you've seen no teachers.
  4. Loop though all of the teachers in the dictionary.
  5. Find out the length of the current teacher's list of classes.
  6. If they have more classes than the current high count, set the high count to the length of the current teacher's classes and set the busiest teacher to be the current teacher.
  7. When we're done with the loop, send back the busiest teacher variable.

Think you can spot your problems and solutions from here?

Ronnie Barua
Ronnie Barua
17,665 Points

In step five is it right?

current_hightest_classes = len(classes(teachers))

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

If your dictionary of teachers is called teachers, and your for loop is for teacher in teachers:, then you'd want current_highest_classes = len(teachers[teacher]) since the value of that teacher's key in the dict is what we want to get the length of.

Ronnie Barua
Ronnie Barua
17,665 Points

sorry I misspelled highest

Ronnie Barua
Ronnie Barua
17,665 Points

It didn't work

for teacher in teachers: current_highest_classes = len(teachers[teacher]) teacher_with_highest_classes = len(teachers[teacher])

return busy_teacher

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

OK, slow down, take it a step at a time.. You're putting the count into both high count variable and the busy teacher variable. And you're returning a busy_teacher variable that I don't see you actually creating anywhere.

def most_classes(teachers):
    busy_teacher = ''
    high_count = 0

    for teacher in teachers:
        if len(teachers[teacher]) > high_count:
            high_count = len(teachers[teacher])
            busy_teacher = teacher

    return busy_teacher
Ronnie Barua
Ronnie Barua
17,665 Points

Thank you so much. I've passed this one and hope to finish rest of the course without any trouble.

Ronnie Barua
Ronnie Barua
17,665 Points

I'm very sorry! Could you please look at this what am I missing here?

def num_teachers(teacher):
count = 0
for x in teachers:
if x == teacher:
count +=1
return count
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Again, your code isn't indented as it should be. Also, as with lists, strings, etc, you can get the length/size of a dictionary with the len() attribute. I'm sure you can find a way to do your function much more easily with that information.

Ronnie Barua
Ronnie Barua
17,665 Points

I have come up with this and doesn't work just keep saying "Try again!"

def num_teachers(teachers):
    count = 0
for num in teachers:
    if len(teachers) == count:
count = num_teachers
return num_teachers
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You still aren't indenting your code correctly. Python's layout is 100% defined by whitespace, so you have to format your code correctly.

After that, can you walk me through your thought process for your code? What are you expecting each line to do?

Ronnie Barua
Ronnie Barua
17,665 Points

Yes you are correct about indent. I often take default, I mean every time I hit Enter key it automatically goes to the place where it suppose to except when I call return, that's when I line that up with default. Sorry I've disappointed you.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

I'm not disappointed in you, Ronnie! I want you to succeed! Just trying to find out what I haven't explained well enough.

Walk me through your logic. What are you expecting each line to do?

Ronnie Barua
Ronnie Barua
17,665 Points

okay, first I named the function than called a var count = 0, than I ran a loop to go over all the teachers. After that I asserted an If statement check the Length of the teachers equal to the counts. I set count equal to num_teachers and finally I called on num_teachers.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

No, no. I know what you did, I want to know why you did them.

Like, why do a for loop there? Why do an if statement inside of it? What are you checking and for what reason?

Ronnie Barua
Ronnie Barua
17,665 Points

Well, that's how I've been counting in the past. I always look at it like a formula to get something. Honestly, I don't even know when and where a loop or if stat. fits right. That's why I am sticking with your class so that I can learn what I need to. Thanks!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

That's a good formula for counting if you need a condition. Like, only count the items in this list where they have a red shirt. When you don't have that condition, though, len() works better.

This step of the challenge doesn't need if or for statements since there's no condition to work through. You just need to get the number of keys in the dictionary.

Ronnie Barua
Ronnie Barua
17,665 Points

Thanks again. I am going to try that. If I remember correctly I tried just len() and didn't work.

This is my solution to this problem. It's quite simple to understand and it's efficient.

def most_classes(my_dict):
  teachers_list = {}
  for teacher in my_dict:
    teachers_list[teacher] = len(my_dict[teacher])
  return max(teachers_list, key=teachers_list.get)