Python Object-Oriented Python Advanced Objects Morse code printer

Alexander Besse
Alexander Besse
Front End Web Development Techdegree Student 14,463 Points

Any Suggestions?

The error only says "Bummer: Try again!"

morse.py
class Letter:
    def __init__(self, pattern=None):
        self.pattern = pattern
    def __str__(self):
        string = ''
        for item in self.pattern:
            if item == '.':
                string += 'dot-'
            elif item == '_':
                string += 'dash-'
        return string[:-2]

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

1 Answer

Steven Parker
Steven Parker
149,192 Points

You are very close! The one issue is that a slice stops just before the end index, and doesn't include it. So you want to slice up to the last character instead of the 2nd to last:

        return string[:-1]
Alexander Besse
Alexander Besse
Front End Web Development Techdegree Student 14,463 Points

Ah, thank you! I though [:-2] would just slice off the last character (stopping at the second to last character). Thank you for the help.