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

Joëlle Galloux
Joëlle Galloux
556 Points

Code is correct but the console doesn't return any result.

Hi there!

For this challenge, I know that my code is correct as I have passed the task.

But I wanted to try it out, so I entered it in a new file in Workspaces and PyCharm. ran the code but I am not getting any result. In Workspaces, I get the following error message: "bash: syntax error near unexpected token"

i wrote the following in the console:

num_teachers({'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'], 'Kenneth Love': ['Python Basics', 'Python Collections']})

Any idea what could be the issue?

When typing the whole thing in the console, I get the correct results. Just not when I run the code from a file.

Thanks!

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(teachers):
    count = 0
    for teach in teachers:
        count +=1
    return count

3 Answers

Not sure if the typo @Wade Williams is referencing was the cause of your issue but just FYI when running from a .py script in the terminal, you wont see anything being returned from a function. However, You can write into your code:

some_variable = num_teachers({'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'], 'Kenneth Love': ['Python Basics', 'Python Collections']})
print(some_variable)

You will then see what is being returned from the function. Thats the great thing about return. It can be assigned to a variable and used elsewhere.

Additionally, this is not important but un-used variables in a for loop like teach can be replaced by an underscore character in Python.

for _ in teachers:

for example.

Wade Williams
Wade Williams
24,476 Points

Looks like you have a syntax error, missing the opening curly brace and closing parenthesis, this worked for me with your code:

num_teachers({'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'], 'Kenneth Love': ['Python Basics', 'Python Collections']})
Joëlle Galloux
Joëlle Galloux
556 Points

Thank you so much for your quick response!

@Wade: Actually, I made the typo when I copy-pasted from my code. Not sure what happened there. Just edited it now :)

@Timothy: Right! I think I misunderstood the difference between print() and return. It's working now. And thanks for the additional tip!

No problem! Glad it helped!