154k views
5 votes
Complete the divisible_by(list1, int1) function below. As input, it takes a list of integers list1 and an integer int1. It should return a list with all the integers in list1 where the integer is divisible by int1. For example, divisible_by([2, 9, 4, 19, 20, -3, -15], 3) should return the list [9, -3, -15].

1 def divisible_by(listi, n):
2 # code goes here
3 pass
4
5 if _name == "__main__":
6 # use this to test your function
7 test_list = [2, 9, 4, 19, 20, -3, -15]
8 print(divisible_by(test_list, 3))

1 Answer

4 votes

Answer:

The function is as follows:

def divisible_by(listi, n):

mylist = []

for i in listi:

if i%n == 0:

mylist.append(i)

return mylist

pass

Step-by-step explanation:

This defines the function

def divisible_by(listi, n):

This creates an empty list

mylist = []

This iterates through the list

for i in listi:

This checks if the elements of the list is divisible by n

if i%n == 0:

If yes, the number is appended to the list

mylist.append(i)

This returns the list

return mylist

pass

User Rui Curado
by
4.7k points