What's wrong with my code?


#1

The window is returning this error:
Traceback (most recent call last):
File "python", line 12, in
TypeError: init() takes exactly 4 arguments (2 given)

class Triangle(object):
    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
    number_of_sides = 3
    def check_angles(angle1, angle2, angle3):
        if self.angle1 + self.angle2 + self.angle3 == 180:
            return True
        else:
            return False
mine = Triangle("Yours")
print mine.check_angles(1,2,3)

#2

You need two more arguments, for the angles. :slightly_smiling:


#3

which two arguments? there are three other angles, what are these two arguments you speak of? Also, I'm only initializing an instance of Triangle class, I'm not using angle values for that, so why do I need that? please address all my questions


#4
  1. Python is big on structure, 4 space indentions and blank lines to separate the code. You need a blank line between return False and mine = Triangle("Yours"). That will let the interpreter know that this code block is finished. Else you will get the syntax error.

  2. The check_angles() function won't work without passing "self". you will get:
    NameError: name 'self' is not defined

  3. It tells you that 2 arguments are given because it knows to take itself and the string "Yours" you passed. Try passing 3 integers that equal or don't equal 180. If you use the string you will get TypeError: cannot concatenate 'str' and 'int' objects.

  4. You created the class and initialized it with the variable mine. That is why you need to use the correct number of angle values or else you will get errors.

I didn't do it in Codecademy course,. I don't know what lesson you are on. I copied your code and used the terminal so I can get the error messages. It works with those changes and brings back true or false when the print line is ran. Hope that cleared it up for you.


#5

You left very good advice, except:

Isn't necessary as the interpreter knows from indentation, not blank lines. :slightly_smiling:


#6

I thought that also. I actually tried it every way I could. The interpreter on my computer stopped throwing errors until I put the blank line in the code. I prefer the command line over the GUI so I use it alot. It worked in Python2 and Python3 with the modifications.

PEP 0008 -- Style Guide for Python Code and 4.8. Intermezzo: Coding Style had the guidelines on using whitespace and blank lines. It would help to have some code on hand because the examples they use don't show how it comes together in complete code. It just have functions and results so you don't get the whole picture. Appreciate the reply, it made me refresh myself on the particulars.

Try it out on your computer in the python interpreter and you will see. Blank lines do matter more than you think.


#7

@jr.smith
I use IDLE, as my interpreter.

I've worked alot with classes, and know blank lines don't matter.

Btw: The links you posted are for styling code. Don't affect syntax at all...


#8

Its working now in the window, but codecademy is saying "Oops, try again. Make sure to pass self as an argument to your check_angles method!"

class Triangle(object):
    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
    number_of_sides = 3
    def check_angles(self, angle1, angle2, angle3):
        if self.angle1 + self.angle2 + self.angle3 == 180:
            return True
        else:
            return False
mine = Triangle(1 ,2, 3)
print mine.check_angles(1,2,3)

Btw, thanks jr.smith, I appreciate ur help.


#9

When it works it works. I know that you have to terminate compound/multi-line statements with a blank line in the interpreter. Blank lines are not required in python scripts. IDLE handles many things automatically. It might be style, 9. Classes in the python docs got classes with and without blank lines inside. Check out the code with the blank lines separating classes from variables, functions and iterators.

Don't know why it wouldn't work for me without the blank lines. It helps with readability for me. I use the python2 and python3 interpreter almost exclusively. I guess that is where our breakdown on this issue occurs. I don't use IDLE because it runs in its own interactive namespace. That caused problems in the beginning when I tried to run them in bash so I used the interpreter to test and copy to vim or some other IDE. In the interpreter you have to explicitly import what you need. I really don't know how IDLE handles blank lines so I can't speak on that issue.

Tried the same code with and without my corrections in IDLE and got an invalid syntax error message whien mine is declared no matter what I did. Tried the code in Codecademy and I only needed to add self to check__angles and change "Yours" to three integers to make the code work. I know the python interpreter will throw an error in this case because it has been throwing syntax errors. It stopped when I inserted the blank line to separate the class from the variable declaration and added self to the check_angles definiton. Then it gives an error about the number of arguments. Changed "Yours" to 60, 60, 60 and TRUE is returned, 60, 60, 90 and FALSE is returned.

I'm confused as to why these subtle differences in the code will produce these results being that python 2.7 is used. It has been interesting seeing the results myself. I will say that both of us are correct, depending on your programming environment. @davidkovic should have enough information to handle the subtle differences needed to code in the different environments. I know I have. When it works it works.


#10

So, as I commented previously, the window is working now, the code itself is printing, but codecademy is saying i have an error. Please explain whats wrong!!!


#11

What lesson are you on. I want to do it.


#12

If you weren't aware of this, all questions in the q and a forum have the topics at the top of a post:
19. Introduction to Classes https://www.codecademy.com/courses/python-intermediate-en-WL8e4/3/2?curriculum_id=4f89dab3d788890003000096#


#13

ahh, never mind, i figured it out, in the check_angles method, i already defined the angles, in the class, aiaiiai, this held me back from continuing onto real learning. I KNEW THAT. Just overlooked it AARRRGGGGHHH :angry: Thanks for helping though :slightly_smiling:


#14

Final fixed code, if u r interested:

class Triangle(object):
    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
    number_of_sides = 3
    def check_angles(self):
        if self.angle1 + self.angle2 + self.angle3 == 180:
            return True
        else:
            return False            
mine = Triangle(1, 2, 3)
print mine.check_angles()

#15

The link at the top took me to the beginning. Thanks for the direct link. I put in my code and got the error. After checking the hint, I deleted angle1-3 from the check_angles definition, I got:
Traceback (most recent call last): File "python", line 15, in <module> TypeError: check_angles() takes exactly 1 argument (4 given)
That led me to remove the three integers from the print statement. Those integers were supplied when mine was instantiated. It passes after that.


#16

yeah, realized that myself already....thanks anyways


#17

Look like we was mirroring each other without realizing it. Right before I finished typing you posted.


#18

lol (everything inside this parentheses is to pass the 20 character limit in order to state LOL :slightly_smiling: )