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 trialavereyku
4,021 PointsCode Challenge Error
When I ran this code in Workspaces, it gave the desired output(you can try it if you don't believe me). When I pressed the "check work" button(using the same code), it threw a TypeError: unsupported operand type "+" for int and list.
Thank you for your attention!
def num_teachers(treehouse_dict = {}):
return len(treehouse_dict)
def num_courses(treehouse_dict = {}):
total_val = 0
for key, value in treehouse_dict.items():
total_val = total_val + value
return total_val
4 Answers
KRIS NIKOLAISEN
54,972 Pointsvalue is still a list and can't be added to an integer. I was attempting to show that. The task asks for the total number of courses for all teachers. To get that you will have to count the items in each list. You have the loop set up but aren't counting items. The following will do so:
def num_courses(treehouse_dict = {}):
total_val = 0
for key, value in treehouse_dict.items():
total_val = total_val + len(value)
return total_val
and passes the challenge.
If you don't agree can you post your workspace as a snapshot and the steps you took to test?
KRIS NIKOLAISEN
54,972 PointsI tried your code in a workspace and received the same error as the challenge. The following:
d = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
'Kenneth Love': ['Python Basics', 'Python Collections']}
def num_courses(treehouse_dict = {}):
total_val = 0
for key, value in treehouse_dict.items():
if isinstance(value,list):
print("I am a list")
total_val = total_val + value
return total_val
print(num_courses(d))
results with:
I am a list
Traceback (most recent call last):
File "a.py", line 13, in <module>
print(num_courses(d))
File "a.py", line 10, in num_courses
total_val = total_val + value
TypeError: unsupported operand type(s) for +: 'int' and 'list'
Since value is a list you should use the len() function here as well to get an item count
avereyku
4,021 Pointsthe code you ran isn't my code. Here is my code:
def num_courses(treehouse_dict = {}): total_val = 0 for key, value in treehouse_dict.items(): total_val = total_val + value return total_val
avereyku
4,021 PointsThat is my code