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

tomasmotycka
tomasmotycka
7,158 Points

WhatΒ΄s wrong with my code in code challenge?

Let's use str to turn Python code into Morse code! OK, not really, but we can turn class instances into a representation of their Morse code counterparts.

I want you to add a str method to the Letter class that loops through the pattern attribute of an instance and returns "dot" for every "." (period) and "dash" for every "_" (underscore). Join them with a hyphen.

I've included an S class as an example (I'll generate the others when I test your code) and it's str output should be "dot-dot-dot".

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

        def __str__(self, value):
            printer = []

            for item in pattern:
                if item == ".":
                    printer.append("dot")

                elif item == "_":
                    printer.append("dash")

        return "-".join(printer)


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

1 Answer

Steven Parker
Steven Parker
231,007 Points

I see a few issues:

  • the indentation makes it appear that the __str__ method is inside the __init__ method
  • the __str__ method should take only one argument (self)
  • the "pattern" is part of the instance and should be referenced as "self.pattern"