Wessel Koot
Wessel Koot

Reputation: 11

Finding the divisables of an integer in a list

Hello all i am just starting to code and I am trying to solve the following question: The function below should accept two arguments, an integer and a list with integers

The function should return a list with True and False for all integers in the list by which the first argument is divisable and False otherwise.

For example, the function call:

main(10, [12, 2, 22, 5 ]) 

should return [False, True, False, True]

this is my code:

def main(integer_1, list_1):
    result= “
    for element in list_1:
        if integer_1%element==0:
            result += "True"
        else:
            result += "False"

print(main(10, [12, 2, 22, 5 ]))

However it returns None

Upvotes: 0

Views: 45

Answers (2)

Mahdi_J
Mahdi_J

Reputation: 428

you should return your result at the end of the function like this:

def main(integer_1, list_1):
    result = ''
    for element in list_1:
        if integer_1 % element == 0:
            result += "True"
        else:
            result += "False"
    return result

which will give you this out put if you execute the rest of your code:

FalseTrueFalseTrue

so you might want to add a single space to the end of string representations of "True" and "False" which will give you this output at the end:

False True False True

Upvotes: 0

Sebastian
Sebastian

Reputation: 561

You must return your result and it would be better to use list instead of string for it. Init it with:

your_list = []

and then call:

your_list.append(new_element)

Where new_element is True of False in this case And at the end:

return your_list

Upvotes: 1

Related Questions