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

Michael Sothan
Michael Sothan
8,048 Points

can't import class for testing, gettin "can't read /var/mail"

I'm attempting to solve this code challenge. Most urgently, Problem 1 is: I can't test my code as I can't get my class to import in Workspaces. I'm first typing " python morse.py " which is working, then "from morse import Character " and I'm getting the "can't read /var/mail" error every time. I saved the morse.py file in the outermost directory, under the OOP directory. I don't know what is the correct name of that directory. I was trying to manually change to it with "cd ~"

Problem 2: I don't understand what the purpose of the code challenge is. What is the program supposed to do? Is the parent class supposed to have a method that allows a child class to be created for each letter in the alphabet that outputs the Morse code of that letter?

Thanks

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

    def __str__(self, pattern)    
        for item in pattern:
            if item == '.':
                morse = "dot"
            elif item == '_':
                morse = "dash"
        print(morse)


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

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

To problem 1: you have two typos in your code

  • missing colon in def __str__(self, pattern)
  • indent 3 instead of 4 in for item in pattern:

Additionally, the __str__ method probably should not have a pattern parameter. Instead, inside the method reference self.pattern. The __str__ method should return a str that contains all of the dots and dashes in the letter encoding. As written, it only "prints" the last dash or dot.

Problem 2: I don't understand what the purpose of the code challenge is.

The challenge is to explore:

  • creating a derived class from a parent class,
  • overriding a parents method with a local method, and
  • using super() to call the parent's method

What is the program supposed to do? Is the parent class supposed to have a method that allows a child class to be created for each letter in the alphabet that outputs the Morse code of that letter

As typical in trying to create an educational example, a contrived scenario may not seem practical, as s = S() could easily be written as s = Letter(['.', '.', '.']).

This does imply the application would be to create a class for each letter in the alphabet. May not be of much real value.

Post back if you wish more help. Good luck!

Michael Sothan
Michael Sothan
8,048 Points

Thanks. Yes, I knew my code wasn't correct, but wanted to be able to play with it in the console of Workspaces to try to figure it out. I gave up importing the class and just imported the file name and it seemed to run.

Thanks for the explanation on the rationale behind the exercise.