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 (2016, retired 2019) Dictionaries Teacher Stats

Anibal Marquina
Anibal Marquina
9,523 Points

I 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

teachers.py
# 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

Hello 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
Anibal Marquina
9,523 Points

hello 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.

Anibal,

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
Anibal Marquina
9,523 Points

thanks 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!

          <p>
def num_courses(teachers):           
    n_courses = []                        
    for courses in teachers.values():  
        for course in courses:
            n_courses.append(courses)
    return len(n_courses) 
</p>
          ```

Mr. Leo, Thank you for this solution. Thank you very much.

Anibal Marquina
Anibal Marquina
9,523 Points

That would be helpfull! thanks

I edited original answer accordingly.

Marius-Catalin Tablet
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree
Marius-Catalin Tablet
Python Web Development Techdegree Student 3,857 Points

def 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...