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 trialAnibal Marquina
9,523 PointsI dont get what is being asked.. TOTAL number of courses for all teachers
I tried the code getting the length of a list with the courses which is 4, and also return the list itself with the corresponding values in the dictionarie for each course.. none of them is working.
Thanks
# 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(teachers):
return len(teachers)
# this code will returned a list with the courses ['jQuery Basics', 'Node.js Basics','Python Basics', 'Python Collections']
def num_courses(teachers):
n_courses = []
for courses in teachers.values():
n_courses.append(courses)
return n_courses
# this code will returned the total number of courses 4
def num_courses(teachers):
n_courses = []
for courses in teachers.values():
n_courses.append(courses)
return len(n_courses)
3 Answers
leonardbode
Courses Plus Student 4,011 PointsHello Anibal,
So, what happened was you were adding the number of values from the dictionary, but not the number of courses within those values!
Here is my code, see if you can understand the difference to yours:
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
If you have any other questions I will update my answer, if you do not have any other questions:
Remember to upvote and to choose the best answer so that your question receives a checkmark in forums.
Kind regards,
Leo
Anibal Marquina
9,523 PointsThat would be helpfull! thanks
leonardbode
Courses Plus Student 4,011 PointsI edited original answer accordingly.
Marius-Catalin Tablet
Python Web Development Techdegree Student 3,857 Pointsdef num_courses(dict): total = 0 for k,v in dict.items(): total+=len(v) print(total) return total
I don't understand why it's returning me a "Bommer" message... can you please have a look over my function from the above and help me with some suggestions? Perhaps I am missing something...
Anibal Marquina
9,523 PointsAnibal Marquina
9,523 Pointshello Leonard
thanks for your answer.
Im aware both functions are the same name. I traid those 2 ways for the num_courses.
the challeng says: That one wasn't too bad, right? Let's try something a bit more challenging. Create a new function named num_courses that will receive the same dictionary as its only argument. The function should return the total number of courses for all of the teachers.
the secons function give me a list with the name of the courses for all teachers. the last one gime me the total number of courses for all teacher, this means 4.
but any of those is the correct answer.
leonardbode
Courses Plus Student 4,011 Pointsleonardbode
Courses Plus Student 4,011 PointsAnibal,
If you'd like I could post my solution to the problem?
You could then read my code to understand what should be done, and if you have any further questions I will update my answer.
Regards,
Leo
Anibal Marquina
9,523 PointsAnibal Marquina
9,523 Pointsthanks Leonard.
Yours is working, also i modified mine like this and is also working. I still dont get it why the answer for the total of courses is 2 instead of 4!
eodell
26,386 Pointseodell
26,386 PointsMr. Leo, Thank you for this solution. Thank you very much.