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

Bob Allan
Bob Allan
10,900 Points

New object doesn't work. Please take a look.

This complies in PyCharm just fine... why doesn't it pass the test?

class Letter:
    def __init__(self, pattern=None):
        self.pattern = pattern

    def __str__(self):
        s = ""
        for el in self.pattern:
            if el == ".":
                s += "dot-"
            if el == "_":
                s += "dash-"
        if len(s) > 0:
            return s[:-1] #removes extra hyphen from end of string
        return s


class S(Letter):
    def __init__(self):
        pattern = ['.', '.', '.']
        super().__init__(pattern)
Cheo R
Cheo R
37,150 Points

I copied and pasted your code and it passed.

Try refreshing the page.

1 Answer

Louise St. Germain
Louise St. Germain
19,424 Points

Hi Bob,

The code itself is fine, but the formatting has a mix of spaces and tabs, and that's why it isn't working in the challenge (Python is really sensitive to this sort of thing). In your code above, you had tabs in front of "def _ _ str(self) _ _:", and also in front of the "if len(s) > 0:" line. If you replace the tabs at the start of those two lines with the appropriate number of spaces, your code will work! (Shortcut: copy and paste your code from this forum post, since it already takes care of turning tabs into spaces to display it on the webpage!)

To avoid this kind of issue, check your settings on PyCharm to make sure it's set to turn tabs into spaces. Also, if you install a Python code linter, that definitely makes it easy to spot these kinds of problems. (I'm using flake8 on Sublime Text. I don't have experience with PyCharm, but for sure there are many linters you can either turn on or install on PyCharm.)

I hope this helps! :-)