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

Kong Tat Ong
Kong Tat Ong
5,145 Points

How do I stop it from adding the last hyphen for the last element?

I don't know what I am doing wrong, but I assume it is due to my last element adding an extra hyphen. Is there some other problem behind my code? Please do tell me. The way that I cannot see my output really annoys me because I can't troubleshoot or understand where I went wrong. All it does is tell me "Bummer: Try again!"

morse.py
class Letter:
    def __init__(self, pattern=None):
        self.pattern = pattern
    def __str__(self):
        str stuff = ''
        for i in self.pattern:
            if(i === '.'):
                stuff += 'dot-'
            elif(i === '_'):
                stuff += 'underscore-'



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

1 Answer

Steven Parker
Steven Parker
231,269 Points

Yes, the extra hyphen is a problem, but there are a few others:

  • in Python, you don't put a type in front of a variable name to create it
  • there's no "===" operator in Python, use "==" for comparisons (are you also learning a different language?)
  • underscores in the pattern should be converted to the string "dash" instead of "underscore"
  • the method needs to return the string after it finishes making it

As for the hyphen, two possible fixes come to mind:

  • use a slice to remove the last character after the loop is done
  • build a list first, then convert it to a string while adding hyphens with "join"