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

stuck on morse code print challenge

Hello,

Unlike previous tasks, I am really lost with this one. I have rewatched the videos, and I still feel that I am lost.

I would like some assistance or hint what I need to do.

I also have a question. How to run and test this in workspace.. I made a new file in workspace with exact code and tried to test it like this..

treehouse:~/workspace$ python
Python 3.5.0 (default, Jul 13 2017, 17:35:34)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.

from a import letter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'letter'
from a import Letter
from a import S
Letter([".", ".", "", "."])
<a.Letter object at 0x7fbf3be3f080>
S([".", "."])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init
() takes 1 positional argument but 2 were given
pattern = [".", ".", "
", "."]
Letter(pattern)
<a.Letter object at 0x7fbf3be3f048>
S(pattern)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: init() takes 1 positional argument but 2 were given
pattern = Letter([".", ".", "_"])
pattern
<a.Letter object at 0x7fbf3be3f080>
str(pattern)
'<a.Letter object at 0x7fbf3be3f080>'
S(pattern)
Traceback (most recent call last):

morse.py
class Letter:
    def __init__(self, pattern=None):
        self.pattern = pattern

    def __str__(self):
        self.results = ""
        self.pattern = pattern
        for i in self.pattern:
            if i == ".":
                self.results += "-dot"
            if i == "_":
                self.results += "-dash"

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

3 Answers

Steven Parker
Steven Parker
229,921 Points

I think I spot a few issues:

  • the __str__ method doesn't take a pattern argument, it should use self.pattern as it is
  • and the end of the method, you need to return something
  • I noticed that the words all have hyphens in front, but the very first one of a group should not

Hello, Mr.Packer

I would like to begin thanking you for your interest and time.

I saw your suggestions and tried to fix them like this..

class Letter:
    def __init__(self, pattern=None):
        self.pattern = pattern

    def __str__(self):
        self.results = ""
        for i in self.pattern:
            if i == ".":
                self.results += "-dot"
            if i == "_":
                self.results += "-dash"
        del self.results[0]    
        return self.results

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

I wish I knew how I could test this in workspace, so I could try to write better and fix some issues before asking again, but I dont know how..

With the new code, I am still getting bumer: Try again. Reading the code I have written seems right but it seems that I am failing to see something.

Steven Parker
Steven Parker
229,921 Points

I don't think you can del characters from a string. But you might create a new string with different characters using a slice.

I'm not a big fan of using workspaces for challenges. It's easy to misinterpret the goal and get frustrated by code that seems to "work".

You are totally right you can not use del in string! I forgot for a second that string is immutable objects. Thank you very much for all your help. I used slice method and with your help I solved.

Well, I usually test my codes in eclipse. But I cant type any code on console, so I tried using workspace. If I were to write this code in the eclipse, with return method replaced with print and say I want the results of [".", ".", ".", "", ""] how would I test it?

Steven Parker
Steven Parker
229,921 Points

The challenges usually don't require a lot of sophisticated debugging and you can work directly in the challenge environment. It also helps you learn to "think like the computer".