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..:)