I don't understand this return statement - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: I don't understand this return statement (/thread-19688.html) |
I don't understand this return statement - 357mag - Jul-10-2019 This book I'm working out of talks about making a boolean function. It works good and here is the code: def is_even(number): if(number % 2 == 0): status = True else: status = False return status number = int(input("Enter a number: ")) is_even(number) if is_even(number): print("The number is even") else: print("The number is odd")What I don't understand is at the bottom of the function the line that reads return status. Where is status going? There is no variable that status is being assigned to. And when I look at the is_even if statement structure there is no mention of status there either. So I don't understand where status is going and how it's being checked in the if statement. RE: I don't understand this return statement - jefsummers - Jul-10-2019 status is the name of the variable assigned True or False by the if statement in the function definition. Return then returns the value of status to the calling function. This can then be assigned to a variable or used in an expression. After the input statement, the function is called and nothing is done with the result. In the if statement after that, the function is called a second time and the result is used as the condition for the if statement. So, the line after the input statement is redundant. That may be your source of confusion. RE: I don't understand this return statement - 357mag - Jul-10-2019 The line after the input statement is where we are calling the function. I don't see how that can be redundant. We need that line otherwise the function never gets called. So return status returns either a True or False value to the line is_even(number). Seems strange how a result can be returned to a line and it never gets assigned to anything. Unless status gets returned to number: if is_even(number): really ends up being if is_even(False): RE: I don't understand this return statement - metulburr - Jul-10-2019 (Jul-10-2019, 05:57 PM)357mag Wrote: The line after the input statement is where we are calling the function. I don't see how that can be redundant. We need that line otherwise the function never gets called. no your calling that function twice. This is your second call Quote:if is_even(number): This function call is doing nothing and can be commented out (the second line here) Quote:number = int(input("Enter a number: ")) is_even(number) IF you wanted to use that one you would use the return value like this value = is_even(number) ... if value:The thing to remember is this piece of code gets replaced with the return value is_even(10) -> True Which in the if condition results to if True:which executes the if condition Where as this is_even(11) -> False which in the if condition results to if False:which does not execute the if condition and falls to the else clause. So because your first call to is_even function here Quote:does not use the return value at all, there is no point to it being there.number = int(input("Enter a number: ")) is_even(number) It is the same as doing this number = int(input("Enter a number: ")) True RE: I don't understand this return statement - perfringo - Jul-10-2019 Nothing to add to metulburr excellent explanation. In programming same result can be achieved in different ways. Alternative approach without function and if-else, requires 3.6 <= Python: num = int(input('Enter a number: ')) print(f"The number is {num % 2 and 'odd' or 'even'}")If the objective is to print out whether number is odd or even one can skip function, if-else statement. |