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 trialDaniel Tompkins
5,591 Pointsdef stats Challenge Task 5
Hello, my code seems to work in workspaces; but, I don't understand why it's throwing the "Bummer! Try again!" error when I try and check it.
# 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(teacherdict):
num_keys = []
for teachers in teacherdict:
num_keys.append(teacherdict.keys())
return len(num_keys)
def num_courses(teacherdict):
course_count = 0
for items in teacherdict:
num_lists=teacherdict[items]
for items in num_lists:
course_count += 1
return course_count
def courses(teacherdict):
course_list = []
for items in teacherdict:
list_of_courses=teacherdict[items]
for items in list_of_courses:
course_list.append(items)
return course_list
def most_courses(teacherdict):
new_dictionary = {}
for teacher in teacherdict:
courses = teacherdict[teacher]
course_count = 0
for lists_of_courses in courses:
course_count += 1
new_dictionary[teacher] = course_count
most_courses = 0
for teachercoursecount in new_dictionary:
if new_dictionary[teachercoursecount] > most_courses:
most_courses = new_dictionary[teachercoursecount]
winner = teachercoursecount
return winner
def stats(teacherdict):
inner_list = []
list_of_lists = []
for key in teacherdict:
inner_list.append(key)
inner_list.append(len(teacherdict[key]))
list_of_lists.append(inner_list)
inner_list = []
return list_of_lists
5 Answers
Umesh Ravji
42,386 PointsHey Daniel, it's just the indentation issue with the first line def stats:
def stats(teacherdict):
inner_list = []
list_of_lists = []
for key in teacherdict:
inner_list.append(key)
inner_list.append(len(teacherdict[key]))
list_of_lists.append(inner_list)
inner_list = []
return list_of_lists
Devon Lawler
4,995 PointsAny insight as to why this didn't work? It seemed to work fine in testing it in Workspaces..
def stats(course_list):
teacher_list = []
for key in listing.keys():
if key not in teacher_list:
teacher_list.append([key, len(listing[key])])
return teacher_list
Umesh Ravji
42,386 PointsHi Devon, if you are having problems it would be best to start your own forum thread, otherwise no one may actually see that you need help. The problem is that your parameter is named course_list
, but inside your function you are trying to use listing
instead.
def stats(course_list):
teacher_list = []
for key in course_list.keys():
if key not in teacher_list:
teacher_list.append([key, len(course_list[key])])
return teacher_list
Devon Lawler
4,995 Pointsargh - swapped the name of the list after working on it in workspaces. Thank you for the quick fix and advice posting elsewhere Umesh. Cheers
Vedang Patel
7,114 Pointshey Daniel I've refactored all your code except the last one
def num_teachers(dic):
return len(dic.keys())
def num_courses(dic):
sum = 0
for key_value in dic:
sum += len(dic[key_value])
return sum
def courses(dic):
new_list = []
for key_value in dic:
new_list.extend(dic[key_value])
return new_list
def most_courses(dic):
max_len = 0
teacher_with_most_courses = ''
for key in dic.keys():
if len(dic[key]) >= max_len:
max_len = len(dic[key])
teacher_with_most_courses = key
return teacher_with_most_courses
Thiago Cavalcante
1,094 PointsHi Everyone,
I have a doubt about stats task. I created the script equals of Daniel Tompkins, but I did not put "inner_list = []" in the end of "for", as result it didn't work. I'd like to know, why putting the inner_list empty the script backs the results correct?
Thanks in advance
Daniel Tompkins
5,591 PointsDaniel Tompkins
5,591 PointsYea, I realized immediately after posting. haha Thanks!