In this exercise, the variable
fourth is used only for the
return statement. Does
return require a variable?
return does not require a variable. The computation done and assigned to the variable
fourth could have just been done on the
return line since it is not used for a
print() statement in the function. If the results were used for more than the
return, then assigning the result to a variable is helpful.
>>> def foo():
>>> print (foo())
>>> def foo():
>>> print (foo())
>>> print (print(''))
In all the above the return value is not defined, hence,
None. This is not a return value, but what the caller sees. The console on occasion will echo that response when
print is that last instruction/command.
Good that the above post came back around through the notification system so it can be corrected. At the time yours truly was fond of the argument that there is no return value. That changed somewhere along the way some months ago to a different and likely more correct stance on the
None that the caller sees.
* function has a return with a value => value
* function has a return with no value => None
* function has no explicit return => None
Consider the above in a conditional sense in which all cases result in a return value, whether real, or None.
Hi. I’m kind confuse with the use of return in this exercise.
Why we didn’t use a print to show the result of fourth?
Wouldn’t produce the same result?
Yes, and no. The result would be fleeting, and not usable by any part of the program. Only the user will see the outcome. The program will see nothing. With
return the program would have access to the result for further use.
This isn’t there in the lesson, but tried it anyways and got the result, in a different way (wrong according to the system of course).
first= a + b
second= c - d
third= first * second
fourth= third % a
return first, second, third, fourth
As you can see, I returned all of the values, instead of just the fourth. Instead of the result being printed vertically, it got printed horizontally and that too in brackets. Like this-
(3, 1, 3, 0)
(2, 0, 0, 0)
Just how did that happen, when brackets didn’t appear in the result when just fourth was returned.
In Python, a comma separated list is treated as a tuple, even when there are no brackets.
a = 'one', 'two', 'three', 'four'
print (a) # ('one', 'two', 'three', 'four')
The order in which the values are written is preserved.
We can unpack both a list and a tuple, if we know how many variables to supply.
one, two, three, four = a
print (one) # one
print (four) # four
Bear in mind that for this particular exercise we are instructed to only return the final value, but I’m sure you know that.
For all the others, wo have trouble to understand the last task:
“Finally, it should return the third number printed mod
"Finally it should return the remain of the third number divided trough argument
why the code third = print(first * second) giving me a error “TypeError: unsupported operand type(s) for %: ‘NoneType’ and ‘int’” when i am doing third%a
third = print(first * second)
The value assigned to third here is not the resolved value of the expression
first * second. The value assigned is the return value of the
print() function. Any guess what that might be? The answer is in your error message.