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 trialMorten Egge
1,898 Pointsmost_courses function gives me an error message
The code works fine in workspace, but I get an error message here. I have no idea why.
# The dictionary will look something like:
# {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
# 'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Each key will be a Teacher and the value will be a list of courses.
#
# Your code goes below here.
def num_teachers(my_dict):
return int(len(my_dict.keys()))
def num_courses(my_dict):
counter = 0
for value in my_dict.values():
for lstValue in value:
counter += 1
return int(counter)
def courses(my_dict):
course_list = []
for item in my_dict.values():
for v in item:
course_list.append(v)
return course_list
def most_courses(my_dict):
counter = 0
winner = ""
for k, v in my_dict.items():
if len(v) > counter:
counter += len(v)
winner = str(k)
return winner
1 Answer
Greg Kaleka
39,021 PointsYou are SO CLOSE. The only thing you've done wrong, is that as you go through each teacher, when you find a teacher whose course count is higher than the current counter
, you're adding to the current counter
value. So if the first teacher has two courses, counter becomes 2. If the second teacher has 3 courses, the counter becomes 5, If the third teacher has 4 courses, they should be the winner, but 4 is not greater than 5, so they don't win!
Instead, simply set couter
equal to the length of the course list. Just change that one line to:
counter = len(v)
Then we're keeping track of what the longest course list is.
As an aside, try to avoid single-letter variables. It's considered bad practice in python. Instead, use nice descriptive values like for teacher, course_list in my_dict.items()
...
Happy coding!
Cheers
-Greg
Morten Egge
1,898 PointsMorten Egge
1,898 PointsDoh! Of course, Thank you!??