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 trialjalil damian
1,452 PointsThat one wasn't too bad, right? Let's try something a bit more challenging. Create a new function named num_courses tha
hoe do i do it
# 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(techers):
count = 0
for teacher in techers:
count += 1
return count
def num_courses():
4 Answers
Alexandra Barnett
Front End Web Development Techdegree Graduate 46,473 PointsHi Jalil! I can talk you through it, step by step:
- You have the function already defined, but it needs an argument just like the previous stage.
- As the challenge wants to know the total number of courses, there could be a variable to hold that total e.g.
total = 0
- The courses are in the value "part" of the key, value pair in the dictionary so, you could do a for loop to get the values e.g.
for key, value in a_dict.items()
- In the for loop, you could then find the length of the
value
and add it to thetotal
variable you made at the start. - Finally, you can then
return
thetotal
I hope that helps - I put it in words for you, so you could see if you can code it by yourself but, if you get stuck just let me know and I can show you what I did :)
Alexandra Barnett
Front End Web Development Techdegree Graduate 46,473 Pointsdef num_teachers(a_dict):
return len(a_dict)
# define the function taking in a dictionary as the argument
def num_courses(a_dict):
# make a total variable to store the total number of teachers that will need to be returned at the end of the function
total = 0
# loop over the dictionary
for key, value in a_dict.items():
# for each entry in the dictionary, find the length (number of courses) of the value (a list of courses) and add to total
total += len(value)
# finally, return the total
return total
Hopefully this helps to explain what is going on :)
jalil damian
1,452 Pointsok thanks that one was rely hard
jalil damian
1,452 Pointsthanks
jalil damian
1,452 Pointsmy proble was i dent folow the for key, value in a_dict.items():
i dinkt that was a e.g
jalil damian
1,452 Pointsjalil damian
1,452 Pointsok
jalil damian
1,452 Pointsjalil damian
1,452 Pointshelp i don't get it