Python

Rodney Lyster
Rodney Lyster
1,630 Points

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

Rodney Lyster
Rodney Lyster
1,630 Points

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 59,027 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....

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 59,027 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!!