Python Object-Oriented Python Advanced Objects Morse code printer

Alexander Besse
Alexander Besse
4,272 Points

Any Suggestions?

The error only says "Bummer: Try again!"
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 = ['.', '.', '.']

1 Answer

Steven Parker
Steven Parker
143,254 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
4,272 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.