'Returning None after conditional

Trying to return the math.floor of k, which is 2

import numpy as np
import math
j = 1

def k(j):
    k = j + 1.2 
    return k       # 2.2

def foo2():
    num = k(j)
    floor = math.floor(num)
    threshold = 5.1
    a = 22.5094
    i = np.arange(threshold, 50)
    if a in i:
        return floor 
print(foo2())

It worked without conditional

import math
j = 1

def k(j):
    k = j + 1.2
    return k        #2.2

def foo2():
    num = k(j)
    b = math.floor(num)
    return b

print(foo2()) 

If you spot where I made a wrong assumption, do share please.



Solution 1:[1]

Try add line: print(a, i) after: i = np.arange(threshold, 50)

You will see that a doesn't belong to i, and becouse of this if that is in the next line will not execute return function, becouse as we saw earlier there is no any value in list i that is equal to value of a. And if return in function isn't executed then function will return None.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JBJ13