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

Iain Watson
Iain Watson
3,031 Points

morse.py

In each iteration I'm adding "dot" or "dash" to a list. So why does "-".join() put dashes between the letters rather than the words?

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

    def __str__(self):
        morse = []
        for p in self.pattern:
            if p == ".":
                p = "dot"
                morse += p
            elif p == "_":
                p = "dash"
                morse += p
        return "-".join(morse)


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

1 Answer

Ryan S
Ryan S
27,276 Points

Hi Iain,

The issue isn't with the join method, it is actually with the way you are adding items to your morse list. Notice you are using addition assignment (+=). This is actually shorthand for the .extend() method on a list. So it will take all letters in the string and add them individually:

>>> morse = []
>>> p = "dot"
>>> morse += p
>>> morse
['d', 'o', 't']

To add a full string as an item, you need to use append():

>>> morse = []
>>> p = "dot"
>>> morse.append(p)
>>> morse
['dot']