I'm meant to write a function in python that returns a list of multiples of 3 this is the code I wrote:
def get_multiples_of_3(numbers):
list_num = []
if numbers:
for n in numbers:
if(n%3 == 0):
list_num.append(n)
return list_num
else:
return numbers
print(get_multiples_of_3)
but my return gave: <function get_multiples_of_3 at 0x7f00ccd761f0> [3, 6, 9, 15, 30]
I don't understand why the <> part is returned but also my expected values are returned too. the program I put my function into has an inbuilt list
CodePudding user response:
You are not calling the function, you are merely printing its definition.
You need to call the function like so:
print(get_multiples_of_3([1, 2, 3]))
CodePudding user response:
your are giving back the function as object and not the function with parameters as it is defined, that's why you are receiving the internal representation of the object(repl) and not expected return value.
CodePudding user response:
print(get_multiples_of_3([1, 2, 3]))
You have to pass something to the function. And it should be an iterable object, such as a list.
numbers = [1, 2, 3]
def get_multiples_of_3():
list_num = []
if numbers:
for n in numbers:
if(n%3 == 0):
list_num.append(n)
return list_num
else:
return numbers
print(get_multiples_of_3())
If the data 'numbers' available for the function. Place them higher. And call the function without arguments. And don't call the function without parentheses(). Without parentheses, you will get the function object itself.