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

P Hoyt
P Hoyt
1,555 Points

I was able to pass the morse code exercise after a little trial and error. My question is how do I test it in an editor

below is my code, which passed. when I import Letter and S from morse.py, and then enter str(S) i get "<class 'morse.S'> rather than 'dot-dot-dot'. Im sure its just how im calling the function. How do I get 'dot-dot-dot' to be returned by the str function.

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

def __str__(self):
    output = []
    for i in self.pattern:
        if i == ".":
            output.append("dot")
        elif i == "_":
            output.append("dash")
    return "-".join(output)

class S(Letter): def init(self): pattern = ['.', '.', '.'] super().init(pattern)

1 Answer

Wade Williams
Wade Williams
24,476 Points

You're calling the str function on the class itself, what you want to do is create an instance of S then call the str function on that instance.

morse = S()
morse = str(morse)

# dot-dot-dot
print(morse)
P Hoyt
P Hoyt
1,555 Points

Awesome, thank you Wade!