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 trial
Y B
14,136 Pointsappend give me 'None' list?
working through the collections part of the python course and the teacher dict question however when I try to answer the stats question with:
def stats(dct): 7 total_list = [] 8 number = 0 9 for tutor,course in dct.items(): 10 number = len(course) 11 sublist = [tutor,number] 12 total_list = total_list.append(sublist) 13 return total_list
line 12 seems to keep wiping total_list to make it have 'None'
I have no idea why?
Thanks.
4 Answers
Mikael Enarsson
7,056 PointsYour code seems fine (maybe not how I would do it, but that's ok), except for one thing:
In your code, you have the line
total_list = total_list.append(sublist)
Now, it's good that you are using append here, however, you don't need the "total_list = " part. Append odds the item in the parentheses directly. A working solution would look like this (tested):
def stats(dct):
total_list = []
number = 0
for tutor,course in dct.items():
number = len(course)
sublist = [tutor,number]
total_list.append(sublist)
return total_list
Also, for future reference, please have a look at the Markdown Cheatsheet, especially for when you want to post code. It makes your question easier to read and allows us to answer more quickly ^^
Y B
14,136 PointsThanks but unfortunately the answer still isn't accepted - not sure why:
# The dictionary will be something like:
# {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
# 'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Often, it's a good idea to hold onto a max_count variable.
# Update it when you find a teacher with more classes than
# the current count. Better hold onto the teacher name somewhere
# too!
#
# Your code goes below here.
def most_classes(dct):
classes = 0
teacher = ""
for tutor,courses in dct.items():
if len(courses) > classes:
classes = len(courses)
teacher = tutor
return teacher
def num_teachers(dct):
return len(dct)
def stats(dct):
total_list = []
number = 0
for tutor,course in dct.items():
number = len(course)
total_list.append([tutor,number])
return total_list
??
Mikael Enarsson
7,056 PointsYou have to much indentation on the return:
def stats(dct):
total_list = []
number = 0
for tutor,course in dct.items():
number = len(course)
total_list.append([tutor,number])
return total_list #returns total_list at the end of the first pass of the loop (meaning that it only sends back a list with the first teacher/number of courses
You have to have your return after the loop, like this:
def stats(dct):
total_list = []
number = 0
for tutor,course in dct.items():
number = len(course)
total_list.append([tutor,number]) #Jumps back to the top of the loop from here
return total_list #After the loop has gone through every teacher in the dict, return total_list
It's a subtle thing, but it works ^^
Y B
14,136 PointsWow I'm an idiot, thanks - I had been looking at it much too long and had missed the obvious things.
Thanks.