lel
lel

Reputation: 41

Why does this code return none

why does the following code returns none:

j = 22
def test(j):
    if j > 0:
        print('j>0')
    else:
        print('j<0')

Output:

j>0
None

Upvotes: 0

Views: 144

Answers (1)

Dyrandz Famador
Dyrandz Famador

Reputation: 4525

A function in Python always has a return value, even if you do not use a return statement, defaulting to None

Because the test function doesn't return a value, it ends up returning the object None. that's why it ended up printing None Since you do not have a return value specified

you may not use print in your function, but return a string instead

def test(j):
    if j > 0:
        return 'j>0'
    else:
        return 'j<0'

then call it like this: print it when calling the function

print(test(22)) 

see answer's here for more detail

Upvotes: 2

Related Questions