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 Error Correction

I'm really enjoying the Python courses. One improvement I'd like to suggest during the objective tasks, you could try to make the error messages similar to what they'd actually say when running Python in your terminal. That may sound confusing so let me explain my situation.

I'm on the Teacher Stats objective 1 of 4 in Python Collections and I did everything I thought was correct. Here's my original code:

def most_classes(teacher_dict):
  max_count = 0
  best_teacher = ''
  for teacher in teacher_dict:
    for classes in teacher_dict[teacher]:
      count += 1
      if count > max_count:
        max_count = count
        best_teacher = teacher
        continue
      else:
        continue
  return best_teacher

It's wrong. The error I keep getting says, "Where's 'most_classes()'?" So I'm looking at my spelling of most_classes, making sure I have () instead of {} and so on. And I'm looking at what else could possibly make my entire function just not show up at all. I then decide to try this same code in my terminal. The error I get back is, "local variable 'count' referenced before assignment." So I change my code to this:

def most_classes(teacher_dict):
  max_count = 0
  best_teacher = ''
  for teacher in teacher_dict:
    count = 0
    for classes in teacher_dict[teacher]:
      count += 1
      if count > max_count:
        max_count = count
        best_teacher = teacher
        continue
      else:
        continue
  return best_teacher

All I did was add 'count = 0' under the first for loop. And boom, it works. I just wish the objectives could give similar error messages to what actual Python would give.

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

I try to balance my CCs between actual errors and helpful messages. Some people are going to find raw errors to be amazingly unhelpful and some people are going to find "Hey, you didn't give me a dict" to be unhelpful. It's a tough line to walk.

Typically, as the courses progress, both inside each course and inside the track, there will be more instances of me just sending you raw Python error messages.

Thank you both for your feedback on this!

Yeah, I agree. It is definitely a good strategy to run programs locally or in workspaces to find the bug in your code. Code challenges aren't very informative when it comes to errors.

That makes sense. Early on its probably better to have more of the helpful messages since people won't really have too much Python background. Thanks for responding to this Kenneth and thanks for the amazing Python courses.