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 trialEvgeniy Vdovenko
1,346 Pointsdictionaries. 5 of 5
Hi everybody!
Im trying to pass the challenge in dictionaries 5/5.
I
ve done the code and tested it in PyCharm. It works there, but not in here. So i tried to play a bit with naming variables and other staff, but it still works in PyCharm but not here. I guess that im using some method which is not correct for this exercise but i
m not sure.
Please help me out!
# 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 (tc):
teacher_courses = {}
num_teachers = 0
for teacher in tc:
num_teachers += 1
return num_teachers
def num_courses (teacher_courses):
num_courses = 0
for course in teacher_courses.values():
num_courses = num_courses + len(course)
return num_courses
def courses (teacher_courses):
courses = []
for courses_list in teacher_courses.values():
for course1 in courses_list:
if course1 in courses:
continue
else:
courses.append(course1)
return courses
def most_courses (teacher_courses):
max_count = 0
most_courses = str()
for teacher, course in teacher_courses.items():
if len(course) > max_count:
max_count = len(course)
most_courses = teacher
return most_courses
def stats (teacher_courses):
stats = list()
for teachers_name, course in teacher_courses.iteritems():
inner_list = list()
inner_list.insert(0, teachers_name)
inner_list.insert(1, len(course))
if inner_list in stats:
continue
else:
stats.append(inner_list)
return stats
2 Answers
James Shi
8,942 PointsYou're calling iteritems(), which is only in python 2, not 3. Changing it to items() should fix the issue.
Matthieu Tripoli
18,677 PointsYou can simplify the code by doing the following:
def stats(teacher_dict): list = [] for teacher, courses in teacher_dict.items(): # Pulls the teacher and their courses list.append([teacher, len(courses)]) # Add to list the teacher and the number of courses return list
Evgeniy Vdovenko
1,346 Pointsthanks a lot!
Evgeniy Vdovenko
1,346 PointsEvgeniy Vdovenko
1,346 PointsGreat man! That worked out! Thanks a lot!