Tried improving the code in Hint, but it doesn't work. For some reason


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

https://www.codecademy.com/courses/python-intermediate-en-WL8e4/3/2?curriculum_id=4f89dab3d788890003000096#

Not stuck tho.

<In what way does your code behave incorrectly? Include ALL error messages.>

Interpreter returns none, I get a message that says “Oops, try again. It looks like your check_angles method doesn’t return True for valid triangle angle values.”

<What do you expect to happen instead?>

I expected the if statement to return true if given any number of arguments that sum up to three. Although it’s supposed to be a triangle, so this isn’t ideal, but still. Is manually typing angle1+angle2+angle3 the only way to do that? It sort of looks clumsy.



class Triangle(object):
    number_of_sides=3
    def __init__(self, angle1, angle2, angle3):
        pass
    def check_angles(self, *args):
        if sum(args)==180:
            return True
        else:
            return False



#2

It’s best to stick to what the instructions asked you to do (manually adding them). :slight_smile: If you want to try doing it a different way, you can copy your code and experiment with it in a code editor.
Also, you may want to reread the instructions again, did you create your objects like they asked?


#3

I actually already completed the whole Classes lesson and I am just left with input/output. I just like to experiment because that’s how I learn. So again, why doesn’t *args work? How would I handle multiple inputs (10, 20, hundreds of them?) I wouldn’t just type them in manually.


#4

This method only takes one parameter, self. Use the __init__ method to pass your arguments.

From a purist point of view, splat is not very reader friendly and actually discouraged in style guides. Better that the arguments were in a list or tuple and unpacked by the function (if there are to be many of them). That way the list can be inspected while debugging.

Here is an example…

  def __init__(self, *args):
    self.angle1 = args[1]
    self.angle2 = args[2]
    self.angle3 = args[3]

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.