12. inheritance syntax


#1

I don't understand what I did wrong.

badcode.txt (338 Bytes)


#2

Triangle should inherit from Shape (which it does)...

class Triangle(Shape):

This line should be removed:

Triangle = Shape(3)

#3

That screenshot doesn't add up. Did you change the code after running it?
Are there any non-ascii characters in the code?

Try adding this at the bottom of your code:

Triangle(3, 4, 5)
issubclass(Triangle, Shape):

It will not do anything to make you pass but it may raise an error if there is something wrong with your code.

If it's still a mystery, post the code in a way so that we can actually run it, as said, the screenshot is a lie in some way, even if you might not intend for it to be.


#5

Once that stray line is removed the code runs as expected.


#6

That line, while it shouldn't really be there, makes no difference. The class statement runs immediately afterwards and the name Triangle will refer to that class once that statement has finished.


#7

I should have said, passes. The SCT doesn't like that line, it would seem.


#8

It'll pass with it too! I read the sct and I typed in the code to see if there was something going on here that I haven't seen before.. There wasn't. So there's something weird going on here, with most possibilities having to do with that the screenshot is a half-truth.

I'll stick to blaming the screenshot.

Like, one could edit the code after getting the error. <-- common mistake, so most likely
Or one could change what `object` refers to so that it'll trigger that error.
Or there could be some really subtly different character in the code to make something misspelled.
Or the system is really broken, but if the sct is running, then the code probably is too.


#9

Sidebar

I've been so hellbent on putting myself in the position of a beginner for so long, it's becoming evident that I have succeeded. However, now I'm prone to commit newbie errors, as a result. In one sense this is success. Now if I can only get the better informed to meet with this level, and myself not lose touch, there may be some reaching out to newbies.


#10

I've tried it with and without that line before posting this thread.


#11

I clicked save and submit right before taking the screenshot, but here's the copy and pasted code.
Edit: not exactly copied and pasted, but close as I could get

class Shape(object):
    """Makes shapes!"""
    def __init__(self, number_of_sides):
        self.number_of_sides = number_of_sides 
Triangle = Shape(3)

class Triangle(Shape):
    number_of_sides = 3
    def __init__(self, sidel, side2, side3):
        self.side1 = side1
        self.side2 = side2
        self.side3 = side3

#12

after posting that other information you requested. I tried the functions you suggested and discovered the problem.

(self, side1, side2, side3)

the 1 was a lowercase "L"


#13

And now we see why pictures are not always as informative as they might seem. Had we been given straight code to begin with, this conversation would have been over long ago. We were typing what we thought we read, without being able to spot the errant character. As it turns out, this is an asked and answered question with very low value.


#14

In addition to confirming that a picture or an exclusively visual inspection of code is often insufficient for debugging, this example underscores the need to look at messages in the console when code is handed over to the Python interpreter.

I have changed this line ...

def __init__(self, side1, side2, side3):

... to this ...

def __init__(self, sidel, side2, side3):

The console now says ...

Traceback (most recent call last):
  File "python", line 17, in <module>
  File "python", line 10, in __init__
NameError: global name 'side1' is not defined

This tells us that there is an issue with side1, meaning that we should consider what needed to happen to get side1 defined. For that to happen successfully, we need a good method header and a good assignment statement, so now they both merit careful inspection.

It looks like that message did not show up in your console, @hashbrowns14 , maybe because an earlier version of your code had a valid __init__ method. If you refreshed your page, the message would have appeared, after submitting again.


#15

The class wasn't instantiated. The two lines from the sct that I posted would have triggered that though.

I suggest changing your browser's monospace font @hashbrowns14 :stuck_out_tongue:
It's.. consolas? Not an uncommon programming font, but those l's!


#16

Yes, you're correct, @ionatan.


#18

A post was split to a new topic: Please give me correct sentence


#19