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

Python challenge 1 of 4 dictionary; teacher with most class

I got the correct answer with the help from other forums in Treehouse. I don't understand how/when this code compares the 2 teachers and 'return' the 'busy_teacher'. If max count = 0, then won't the first len(teacher_dict[teacher]) being compared to max count will become the answer? When does the comparison of classes occur?

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.
teacher_dict = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms',
                                 'Technology Foundations'], 'Kenneth Love': ['Python Basics', 'Python Collections']}

def most_classes(teacher_dict):
  max_count = 0
  busy_teacher = ''
  for teacher in teacher_dict:
    if len(teacher_dict[teacher]) > max_count:
      max_count = len(teacher_dict[teacher])
      busy_teacher = teacher
  return busy_teacher

2 Answers

Nhat Nguyen
Nhat Nguyen
4,669 Points

The first teacher won't necessarily be the answer of the max_count. For example:

  • If the first teacher has a length of 1, max_count will be updated to 1
  • If the second teacher has a length of 3, max_count will also be updated to 3 (because 3 > 1).
  • However, if the third teacher has a length of 2, max_count won't be updated as 2 < 3 (the current max_count's value)
  • The process keep repeating until the loop ends, and the max_count will end up storing the largest value because it only updates its value if it encounters a teacher's length that is larger than its current value

As a result, you can see that the initial value 0 of max_count is just technically a 'placeholder' value. You can set it to something that is definitely smaller than any length of the teacher in that dictionary (e.g: 0 or a negative number), but not a value like 10, which won't work if all teachers' lengths are lesser than 10 (in that case, the max_count won't be updated at all.)

Please note that this is kind of an important and basic algorithm to know and understand. It will help you solve related problems like find the minimum/maximum value of a list and things like that.

Hope this explains your quesiton. :)

Thank you for your comprehensive response. I don't understand the general property of the 'if' and 'for' loop because I though that for a loop to occur/end, there must either be 'continue' or 'break' under. The 'for' and 'if' loop will repeat until it reaches a specific condition, regardless of whether you add 'break' or 'continue'. Am I right? Sorry if my question sounds repetitive, I've just started coding.

Nhat Nguyen
Nhat Nguyen
4,669 Points

Hi there

The line

  for teacher in teacher_dict

basically means that it will loop through every teacher in the teacher_dict, so the 'break' condition here is when you have visited all the teachers. if len(teacher_dict[teacher]) > max_count in the body of the loop just means that you repeat the same thing for every teacher you visit.

Now I completely understand the loop method. You understood exactly what I was asking. Thanks again!

Nhat Nguyen
Nhat Nguyen
4,669 Points

Glad that I could help! One more thing that I hadn't noticed in your comment:

"The 'for' and 'if' loop will repeat until it reaches a specific condition, regardless of whether you add 'break' or 'continue' "

  • This is not true, the loop will repeat until it reaches a specific condition, but it will stop right away if it sees a break command (e.g: It doesn't care whether it has reached that condition or not. It will just break out of the loop.) For example:
for i in range(10):
     print i
     if i == 5:
           break

The above code prints the number from 0 to 5. You can see that although the terminate condition is when i >= 10, the loop exits when i = 5 because of the break command.

  • For the continue keyword, the loop will not stop, however, it will skip the rest of the loop body for the current iteration, and continue to the next iteration. For example:
numbers = [1, 2, 10, 11, 12]
sum = 0
for number in numbers:
          if number < 10:
                continue
          sum += number

In those lines of code, you're adding up all the numbers that are larger than 10. If the number is lesser than 10, you basically skip the rest of the body of the loop (e.g sum += number will not be executed) and jump to the next number. Please do note that continue is not recommended as it doesn't encourage good programming practices and it breaks the natural flow of the program.

This is a better version without using continue:

numbers = [1, 2, 10, 11, 12]
sum = 0
for number in numbers:
       if number >= 10:
             sum += number