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 trialMichael Moore
7,121 Pointsstats works but not accepted
It is bringing back the correct response. This is what i pass in, and this is what I receive:
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)
<<< [['Maxx Maxx', 1], ['John Love', 2], ['Michael Moore', 4], ['Emmett Vernon', 1], ['Jacob Carroll',
3]]
and here is my actual code:
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
print(teachers_class_list)
return(teachers_class_list)
3 Answers
Steven Parker
231,236 PointsI pasted that into the challenge and it passed. But I did have to supply my own answers to the other 4 tasks to get to the last step. You didn't happen to accidentally remove your first 4 functions, did you?
Otherwise, perhaps it would help if you show your complete code.
Manish Giri
16,266 PointsWhy don't you try using List Comprehension instead?
def stats(tdict):
return [[k, len(v)] for k,v in tdict.items()]
This loops through the key-value pairs in the dict, and for each pair, it produces a list of two elements - the teacher's name, and the number of their courses. These individual lists are then combined together and returned in one outer list.
Steven Parker
231,236 PointsIf he's taking the courses in track order, I don't think that concept has been introduced yet.
Michael Moore
7,121 Pointsyes, I am going down the track in order of how it takes me.
Goekalp Cicek
9,322 Pointsdef stats(b):
l=[]
for i,j in b.items():
l.append([i,len(j)])
return l
Steven Parker
231,236 PointsFYI: According to a moderator, explicit answers without any explanation are strongly discouraged by Treehouse.
Michael Moore
7,121 PointsMichael Moore
7,121 Pointsit passed? Weird, it would not pass for me. I had not removed the first 4 answers at the time of my pasting, i was just posting the part of the section I was having trouble with rather than the whole problem. I did not want to clutter up the page with material that was not relevant to my problem. But next time I will if that makes it easier for others.
I still do not understand why it would not pass for me. I ended up using someone elses code just to pass by the section so I could continue. It kept saying, "Bummer! Try again!"
Steven Parker
231,236 PointsSteven Parker
231,236 PointsAll I can think of is that something about the difference in the first 4 answers (not shown here) affected the last verification.