(Just wondering) Why do we have to use "If--Else" instead of just "return"?


#1



Introduction to classes, num 16--Class it up
https://www.codecademy.com/courses/python-intermediate-en-WL8e4/3/2?curriculum_id=4f89dab3d788890003000096

I had originally thought to use an if/else to return True or False accordingly from the sum, but then I realized that it's a bit redundant. Instead I thought I could just put the boolean statement on one line with the return, assuming it would be evaluated on that line. However, when I try printing the result from calling the method, the console prints out

{bound method Triangle.check_angles of {Triangle object at 0x7f5bad8fe810}}
None

Note: { is really "<" and } is really ">" but the <> do weird things to the text I type

And I assume it's just not evaluating the boolean and returning the text as it is instead of evaluating it.

My question is, why can't I get python to let me evaluate and return boolean expressions on a single line like shown? It does this for variables, right? Why do I have to use the if/else to evaluate booleans, or am I missing another way?


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):
        return self.angle1 + self.angle2 + self.angle3 == 180

mytri = Triangle(120,50,10)
print  mytri.check_angles


#2

@methodplayer68482,
Have you tried using

print  mytri.check_angles()

Without the pair of parentheses-( ) you were getting

<bound method Triangle.check_angles of <__main__.Triangle object at 0x21fa54>>

which is the internal description of your =check_angles()= Method........
and not the result of the execution of this =check_angles()= Method

and it could be the course requiring you to use the IF ELSE....


#3

Oh yeah, adding the parentheses made it work just as I wanted. Thank you!


#4

@methodplayer68482,
As an EXTRA....

the FUNCTION talk

def myFunc( param1, param2):
    # Begin of =myFunc= FUNCTION-BODY
    # this =myFunc= function- has 2 PARAMETERS param1 and param2
    # param1 and param2 PARAMETERS are used 
    # as -local- VARIABLES throughout the =myFunc= FUNCTION-BODY
    print( param1 + " and " + param2 )
    #End of =myFunc= FUNCTION-BODY

If you want to call/execute the myFunc function
you will have to add a pair of parentheses to myFunc
like
myFunc()
As the myFunc function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren")

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++ function with 1 parameter using return-statement

def myFunction( param1 ):
    # //Begin of =myFunction= FUNCTION-BODY
    # //=myFunction= function has 1 PARAMETER param1
    # //this param1 PARAMETER is used as a -local- VARIABLE
    # //throughout the =myFunction= FUNCTION-BODY
    return param1;
    # //End of FUNCTION-BODY

You have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the =myFunction= FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 )

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the print-method to do a display.

theResult = myFunction( 4 )
print theResult

OR directly

print myFunction( 4 )