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

Python Python Collections (2016, retired 2019) Dictionaries Teacher Stats

Kaushal Patel
Kaushal Patel
1,578 Points

teacher name

Code is working fine but it doesnt pass here

def most_courses(data): teacher = list() courses = list() for key in data.keys(): teacher.append(key) for value in data.values(): courses.append(len(value)) i = 0 a = list() while i < len(courses): if (max(courses)) == courses[i]: a.append(teacher[i]) i += 1 else: i += 1 continue return a

teachers.py
# 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(data):
    teacher = 0
    for item in data.items():
        teacher += 1
    return teacher

def num_courses(data):
    course = 0
    for value in data.values():
        for things in value:
            course += 1
    return course

def courses(data):
    courselist = list()
    for value in data.values():
        for things in value:
            courselist.append(things)
    print(courselist)
    return courselist

def most_courses(data):
    teacher = list()
    courses = list()
    for key in data.keys():
        teacher.append(key)
    for value in data.values():
        courses.append(len(value))
    i = 0
    a = list()
    while i < len(courses):
        if (max(courses)) == courses[i]:
            a.append(teacher[i])
            i += 1
        else:
            i += 1
        continue
    return tuple(a)



mydata = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
'Kenneth Love': ['Python Basics', 'Python Collections'], 'Kaushal Patel': ['English Basic']}

num_teachers(mydata)
num_courses(mydata)
courses(mydata)
most_courses(mydata)

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Your code isn't returning a single teacher name, but rather returning a list of teacher names. Your code should return only one single teacher name... the name of the teacher who teaches the most courses. So instead appending that teacher name to a list you might consider making a variable to hold a single name of a teacher. Every time you find a teacher who teaches more courses, replace the name of the currently held teacher with the teacher that teaches more.

Hope this helps, but let me know if you're still stuck! :sparkles:

Kaushal Patel
Kaushal Patel
1,578 Points

Thanks Jennifer, I am saving the name to variable instead of list and it passed.

why I chose the list because it is possible that more than one teacher may have same number of course.

Thanks for your response,

Thanks & Regards, Kaushal