1

Sorry I know it's a terribly easy question. But i don't understand why my code returns None

def fun(x, y):
    ''' takes an orderd list and an another number
    as input'''

    if y in x:
        return print("it's in the list")
    else:
        return print("number is not in the list")

print(fun([2,3,4,5], 5))
Robin Kohrs
  • 655
  • 7
  • 17

2 Answers2

1

print is a function that doesn't return any value in Python. It only print its own arguments to the user on the screen.

Here is revised code:

def fun(x, y):
    '''Takes an ordered list and another number as input'''

    if y in x:
        return "it's in the list"
    else:
        return "number is not in the list"

print(fun([2,3,4,5], 5))

For better readability, it is better to use "long" arguments after "short". Here is my more idiomatic version, just to build better habit:

def contains(item, sequence):
    '''Check if item contains in sequence'''
    if item in sequence:
        return True
    else:
        return False

print(contains(5, [2,3,4,5]))
Mikhail Kashkin
  • 1,521
  • 14
  • 29
0

You print the return value of the print that is returned from function.

def fun(x, y):
    ''' takes an orderd list and an another number
    as input'''

    if y in x:
        return print("it's in the list")
    else:
        return print("number is not in the list")

fun([2,3,4,5], 5)
GCS
  • 199
  • 1
  • 7