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

Right Output for the code challenge But it says its wrong?

I am getting the output "dot-dot-dot".

But I can not complete this challenge because I am getting everytime the following error:

Bummer: Didn't get the right string output. Got: dot-dot-dot for S()

Tried with two different def str(self) methods:

  1. def str(self): string = "" counter = 0 for m in self.pattern: if m == ".": string += "dot" if m == "_": string += "dash" if counter != (len(self.pattern)-1): string += "-" counter += 1 return string
    1. def str(self): return str(self.pattern).replace('.','dot').replace(', ','-').replace('[','').replace(']','').replace('\'','')
morse.py
class Letter:
    def __init__(self, pattern=None):
        self.pattern = pattern


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


    def __str__(self):
        return str(self.pattern).replace('.','dot').replace(', ','-').replace('[','').replace(']','').replace('\'','')

1 Answer

Steven Parker
Steven Parker
231,269 Points

You're close, but:

  • the method was added to "S", but the instructions say "to add a __str__ method to the Letter class ..."
  • they also say that the method should return "dash" for every "_" (underscore)

The "Bummer" may be a bit misleading since the second problem is detected when testing letters other than "S".