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

Gokul Nishanth
PLUS
Gokul Nishanth
Courses Plus Student 581 Points

Didn't get the right output.

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 prints out "dot" for every "." and "dash" for every "". 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):
        hyphen_code = []
        for i in self.pattern:
            if i == '.':
                hyphen_code.append("dot")
            elif i == '-':
                hyphen_code.append("dash")
        return "-".join(hyphen_code)

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

It looks like your elif i = '-' is supposed to be: elif i = '_'

3 Answers

Steven Parker
Steven Parker
229,786 Points

You just have a typo.

On line 10, you have a hyphen ("-") where you need an underscore ("_").

Hello Steven Parker -

I thought with python 3.5 I could use a ternary conditional but can't get it to work. Could you show me how to do it correctly? This assumes there are only dots and dashes and doesn't have to check for other characters.

def __str__(self):
    new_pattern = []
    for a in self.pattern:
        new_pattern.append('dot' if a == '.' else 'dash')
    return '-'.join(new_pattern)

@Steven Parker I keep getting the error: "Didn't get the right string output. Got: dot-dot-dot for S()", even after checking for indentation. Thanks for checking!

Steven Parker
Steven Parker
229,786 Points

Keith Ostertag - that worked for me when I pasted it into the challenge. Are you sure you indented it properly?

class Letter:
    def __init__(self, pattern=None):
        self.pattern = pattern
    def __str__(self):
        string = []
        for letter in self.pattern:
            if  letter == ".":
                string.append("dot")
            if  letter == "_":
                string.append("dash")

        return "-".join(string) 

class S(Letter):
    def __init__(self):
        pattern = ['.', '.', '.']
        super().__init__(pattern)
Jorge Garro Elizondo
Jorge Garro Elizondo
6,274 Points

indentation issues....just paste your code on an external editor and make sure your indentation spaces/tabs are right