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

What am I missing, I can't even get Hi to print in this?

I was trying to do the Python Morse Code question and moved it to the workspace to test and play. I started on an answer, it would run, but nothing would happen. I deleted everything and ask it to just print HI and it wouldn't. So I knew that piece of code wasn't being traced, but I don't know what I am missing.

here is my code, if you can help.

code = []

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

    def __str__(self):
        print("hi")   

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

dash = "-"        
#code.append("try")
#code.append("try")
#codestr = dash.join(code)
print("done")
print(code)
#print(codestr)

All I get when running this is done []

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

There are two reasons why the print doesn't work.

  • there is no active instance of S or Letter where the str method can be called to print
  • The "print" is directed to the console which would only happen for an instance used in a string context:
$ ipython
Python 3.6.8 (default, Oct  7 2019, 12:59:55) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.8.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import letter                                             

In [2]: s = letter.S()                                            

In [3]: print(s)                                                  
hi
------------------------------------------------------------------
TypeError                        Traceback (most recent call last)
<ipython-input-3-0ff1b7208845> in <module>
----> 1 print(s)

TypeError: __str__ returned non-string (type NoneType)

but this will raise an TypeError since the __str__ method is expected to return a str object. It would work better if you changed the str method to:

    def __str__(self):
        return "hi"

Post back if you need any more help. Good luck!!

.

Ok, I am still learning to post, I think i have the code reading right now. Sorry.... update --- and I learn how to edit my first post.

code = []

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

    def __str__(self):
        print("hi")   

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

dash = "-"        
#code.append("try")
#code.append("try")
#codestr = dash.join(code)
print("done")
print(code)
#print(codestr)
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Hi Rodney! Kudos on getting the formatting correct on your post. That is something many people don't catch on with!

As for editing, below each post, comment, or answer there is the "grey ellipsis" (…) to the right of the Add Comment link. Clicking on the ellipsis reveals an Edit option. There is also a "Delete" option for those times....