No execution after return
A function might have multiple return statements. It can return one value in an
ifstatement and another value in an
elsestatement. It’s important to keep in mind that the function stops its execution as soon as it returns a value. As soon as the function reaches a
returnstatement, the program continues its execution from the place where the function was called:
def process_only_even(n): if n % 2 == 1: return 'This was an odd number!' print('Very interesting number ...', n) n += 18 print('Adding 18 will result in:', n) return n
print(process_only_even(5)) # This was an odd number! print(process_only_even(6)) # Very interesting number ... 6 # Adding 18 will result in: 24 # 24
So, the function returns only once, and the program continues its execution from the point where the function was called.
This is especially handy if you can return a value in a loop, and therefore, both the loop and the function will stop at that point and the program will get back to where the function was called from. We can avoid using
breakin loops like that.
We will call a number “special” if it’s even and the sum of its last two digits is 7.
Implement a function
is_special(n)that would return
nis special and
Falseotherwise. If the number is not “special”, the function should also print
Not specialin the output before returning.
The input contains a single integer
n(100 ≤ n ≤ ).
The program should print
nis special and
Nootherwise. In the case of printing
No, the program should also print
Not specialbefore the
Not special No
Time limit: 1 seconds
Memory limit: 512 MB
Output limit: 1 MB