-2

I started learning python and this is my first-ever question on stackoverflow. Question is: Why is Function return false printed when running the code? Which part of my code has it printed out?

def return_false():
    print("Function return false")
    return False
    
def return_true():
    print("Function return true")
    return True

print("test 2")

if return_false() and return_true():
    print("True")
else:
    print("False")
test 2
Function return false  # I don't understand what makes this popped up.
False
CherryDT
  • 25,571
  • 5
  • 49
  • 74
  • 1
    `print("Function return false")` in line 2 – CherryDT Jul 14 '21 at 05:47
  • You call both functions from your `if` statement. Since `return_false` returns `False` the `and` operator [short-circuits](https://stackoverflow.com/questions/2580136/does-python-support-short-circuiting) and the second function (`return_true`) is not called. – Selcuk Jul 14 '21 at 05:48

1 Answers1

0

Firstly,the question needs some editing but as the edit queue is full i can't do the same so i'll answer your question instead.

Your code goes like:

    def return_false():
        print('Function return false')
        return False
 
    def return_true():
        print('true')
        return True

    print('test 2')
    if return_false() and return_true():    # the problem is here
        print('True')
    else:
        print('False')

and the output you were expecting must be:

    test 2
    False

whereas the actual output is:

    test 2
    Function return false
    False

The problem is with the if statement (and mainly with the functions). So, your print('test 2') is responsible for the output test 2 but as you write if return_false() and return_true(): python calls the functions in order to get the return values. It first calls return_false() which not only returns False but also has a print('Function return false') statement enclosed in it so it is executed too and it is responsible for the extra Function return false output in your code.

Finally, heading next the if statement gets the and operator, but as the value for the logical comparision is False it doesn't have to check the second value cause no matter what the second value is the expression will always evaluate to False (as True and False or False and False will both result in False).Therefore, the code under the if block doesn't get executed and the program reaches the else statement and execcutes the print('False') statement, which is responsible for False as the last output.

To fix this, you code modify your code as:

    def return_false():
        # print('Function return false') # remove this line and use print(return_false()) instead to learn that the function returns a False value
        return False
 
    def return_true():
        # print('true') # remove this too and use print(return_true()) instead
        return True

    print('test 2')
    if return_false() and return_true():
        print('True')
    else:
        print('False')

This code will output:

    test 2
    False

I hope the answer was helpful...Happy codeing..:)

Ajay Singh Rana
  • 573
  • 4
  • 19