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 trialAnders Axelsen
3,471 PointsTask 1: How do I convert the keys in counts from strings to ints?
I'm not sure, if I have both the name
s and the field
s placed in counts. If so, how do I seperate, before making them into ints?
# 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(dictionary):
counts = []
teachers = [{"Name": "Kenneth", "Field": "Python"}]
for key in teachers:
counts.append(len(teachers[key]))
return counts
Posting the script here, makes me reconsider the way I have organized teachers
. The green color sparks the fire in new ideas.
3 Answers
Steven Parker
231,248 PointsA "new idea" would be good. Here's a few hints:
- don't create your own list of teachers, you'll get them from the dictionary passed in
- the instructions say your "function should return an integer" (not a list)
- you won't need a loop, just return the number of teachers (records) in the dictionary
Herman Brummer
6,414 Pointsdef num_teachers(dictionary_list):
teacher_list = dictionary_list.keys()
number_of_teachers = 0
for teachers in teacher_list:
number_of_teachers += 1
return number_of_teachers
Herman Brummer
6,414 Pointsdef num_teachers(teachers_courses):
return len(teachers_courses)
Steven Parker
231,248 PointsRight, but I was going to let folks work it out from the hints.
Besides, Treehouse discourages the posting of explicit solutions without explanations.
Herman Brummer
6,414 PointsSry Steven did not mean to step on your toes, I was not aware of this,
I think they might have solved it ages ago since it was a month ago.
Steven Parker
231,248 PointsI imagine Anders has moved on long ago, I was just thinking about other students who might read this now and in the future when they are at the same point in the course. You might consider deleting the "spoiler" for their benefit.
Anders Axelsen
3,471 PointsAnders Axelsen
3,471 PointsThank you for the reply, Steven. :)
So - where I'm at, I've learned,
Teachers
, as it will - when I know how to - return the key as an integer.I think, I will run the dictionary course through, until I get a better comprehension of the syntax.
Steven Parker
231,248 PointsSteven Parker
231,248 PointsGood so far! Here's another hint: Like you can do with a list, you can use the len() function on a dictionary to get the number of items in it.