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 trialjackyj
9,969 Pointsteachers.py Challenge Task 5
According to Python Shell, the code is working. However, when I tried to check my work in the challenge, it says "It looks like Task 4 is no longer valid.". I am not sure what I did wrong. I need an extra eye to look at my code. 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.
dic = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'], 'Kenneth Love': ['Python Basics', 'Python Collections']}
def num_teachers(dic):
return len(dic)
def num_courses(dic):
sum = 0
for value in dic.values():
sum += len(value)
return sum
def courses(dic):
liso = []
for value in dic.values():
liso += value
return liso
def most_courses(dic):
max_count = 0
teacher_most_classes = " "
for key in dic:
if len(dic[key]) > max_count:
max_count = len(dic[key])
teacher_most_classes = key
return key
def stats(dic):
list_name = []
list_numcours = []
for key in dic:
list_name.append(key)
list_name.append(len(dic[key]))
list_numcours.append(list_name)
list_name = []
return list_name
6 Answers
Stuart Wright
41,120 PointsI think you've just made the same mistake as you made in task 4, in that you're simply returning the wrong variable. Try returning list_numcours instead.
Here's my solution which follows the same logic as yours but uses different names for lists which perhaps makes it a little easier to visualize:
def stats(dic):
outer_list = []
for key in dic:
inner_list = []
inner_list.append(key)
inner_list.append(len(dic[key]))
outer_list.append(inner_list)
return outer_list
Sakari Huuskola
1,139 PointsThis is how I made the last challenge, in my opinion this is nicer to look at
def stats(teachers):
namelist = [] #empty list
for teacher, courses in teachers.items(): #for loop to go through the dictionary
namelist.append([teacher, len(courses)]) #adding stats to the empty list
return namelist
Christopher Gunawan
10,754 PointsI like this one the best! Good stuff
Stuart Wright
41,120 PointsThe error message is correct - task 4 doesn't pass because it is wrong. You need to return 'teacher_most_classes', not 'key'.
As for task 5 itself, I can tell from just the last two lines that it will return an empty list every time.
jackyj
9,969 PointsWhat would I need to change for task 5? I have tried several approaches, but none of them seem to work.
Opanin Akuffo
4,713 PointsTry this, worked for me and its shorter.
def stats(dic):
table = []
for key in dic:
count = len(dic[key])
table += [[key, count]]
return table
Michael Moore
7,121 PointsMine isn't accepted in Treehouse submission to pass the challenge, but it results in the correct outcome when running the code in workshops.
def stats(dictionary):
teachers_class_list = list()
i = 0
for key, value in dictionary.items():
teachers_class_list.insert(i, [key])
if isinstance(value, list):
teachers_class_list[i].append(len(value))
else:
teachers_class_list[i].append(1)
i+=1
return(teachers_class_list)
when passing in
dictionary = {"John Love": ["python", "cyclops"], "Emmett Vernon":"c++", "Jacob Carroll":["fixing", "sinks", "handyman"], "Michael Moore":["ruby", "calculus", "physics", "meaning of life"], "Maxx Maxx":"css"}
stats(dictionary)
it results in:
[['John Love', 2], ['Emmett Vernon', 1], ['Michael Moore', 4], ['Jacob Carroll', 3], ['Maxx Maxx',
1]]
Can anyone tell me why it wasn't accepted?
f4u57
6,743 PointsIt wasn't easy but I did it!
def stats(my_dict):
list = []
for teacher, course_list in my_dict.items():
num_course = len(course_list)
list.append([(teacher), num_course])
print(list)