Yeo'
Yeo'

Reputation: 7

Why the print statement is not working in a function that return boolean?

may I ask why the print statement in the function below is not working? If there is something I have done wrong pls let me know.Thank you

def c_greater_than_d_plus_e(c ,d ,e):
    if c > d + e:
        return c > d + e
        print("c is greater than d plus e ")

print(c_greater_than_d_plus_e(3 ,1 ,1))

output: True

Upvotes: 0

Views: 30

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522501

Once you call return from a Python function, nothing after that will execute. Try this version:

def c_greater_than_d_plus_e(c ,d ,e):
    if c > d + e:
        print("c is greater than d plus e ")
        return c > d + e

print(c_greater_than_d_plus_e(3 ,1 ,1))

This prints:

c is greater than d plus e 
True

Upvotes: 3

Related Questions