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 trialMahmoud Othman
2,602 Pointshelp with def coureses
Hello can any tell em what's wrong with my courses function ?
# 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(teacher_dict):
return len(teacher_dict)
def num_courses(teachers):
total = 0
for value in teachers.values():
# for the number of courses within that value
for course in value:
total += 1
return total
def courses(teachers, courses):
list_courses = []
for value in courses.values:
for course in value:
list_courses.extend(course)
return list_courses
2 Answers
Philip Schultz
11,437 PointsHey Mahmoud, I see a few things. Your function should only have one parameter, because it's receiving only one dict argument. Also, in the for loop, you want to access the values of the parameter, remember when you use methods , they have to have parentheses. Right now you are saying some_dict.value, but you want to say some_dict.values(). Take a look at the code below and let me know if you have any questions.
def courses(teachers):
list_courses = []
for value in teachers.values():
for course in value:
list_courses.append(course)
return list_courses
Julia Beckwith
2,526 PointsYou're very close! Since they are all just taking a single dict as an argument, you can change the argument for courses to just one argument. After that, you will only need one "for" statement. Also, don't forget the parentheses at the end of courses.values()!
Philip Schultz
11,437 PointsPhilip Schultz
11,437 PointsHey, here is another way to do it. This way you only have to use one for loop.
I noticed how you were trying to use extend, which I forgot about. So, I looked up how to use it again and it makes the code more efficient and easier to read. At least in this case.