So I’m trying to make a list of numbers that are multiples of 3, this is my code:

multiples_of_3 = []
for value in range(3, 31):
    number = value % 3 == 0
    multiples_of_3.append(number)

print(multiples_of_3)

But it transforms the results into booleans, which throws me a list with false and trues instead of numbers, what I’m doing wrong? I know comprehension lists exist and that this could be done more efficiently but well, I’m trying to learn and writing it as a comprehension list doesn’t make it easier.

  • @nour
    link
    5
    edit-2
    1 year ago

    In that case, I think you have to use a different approach, and rather than iterate over all the numbers and filter out the ones that aren’t multiples of 3, you can iterate over the multiples of 3 and append every number you iterate over to the array. (Which is what snek_boi’s answer suggests.)

    Code for a possible solution, I'm hiding it behind a spoiler because I assume you want to try yourself
    multiples_of_3 = []
    for value in range(3,31,3):
        multiples_of_3.append(value)
    
    print(multiples_of_3)