0

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
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
lel
  • 41
  • 8

1 Answers1

2

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

Community
  • 1
  • 1
Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40