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@lemmygrad.ml
    link
    fedilink
    71 year ago

    Your problem is this statement:

    number = value % 3 == 0
    

    It gets evaluated in this order:

    number = (value % 3 == 0)
    

    So, first value % 3 == 0 gets evaluated to True or False, and then that gets assigned to the variable number.

    If you only want to append numbers that are multiples of 3, you would need an if-statement, like this:

    multiples_of_3 = []
    for value in range(3, 31):
        if value % 3 == 0:
            multiples_of_3.append(value)
    
    print(multiples_of_3)
    
    • Soviet SnakeOP
      link
      fedilink
      31 year ago

      I thought of using an if, but I’m reading a book called “Python Crash Course” and he hasn’t gone over if statements yet, so I thought there has to be a way to express it only using for statements. I have some more knowledge than what was already presented to me in the book from college, but I’m trying to follow the book’s reasoning so far.

      • @nour@lemmygrad.ml
        link
        fedilink
        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)
        
  • @snek_boi@lemmy.ml
    link
    fedilink
    41 year ago

    The range function has a “step” parameter. You can use it. That way you go from 3 to 31 stepping 3 by 3.

  • You’re assigning value % 3 == 0 to number, which evaluates to a boolean instead of a number. Instead of adding the result of that boolean expression, I’d use that boolean expression as a condition for an if and, if true, append value.

    • Soviet SnakeOP
      link
      fedilink
      31 year ago

      I thought of using an if, but I’m reading a book called “Python Crash Course” and he hasn’t gone over if statements yet, so I thought there has to be a way to express it only using for statements. I have some more knowledge than what was already presented to me in the book from college, but I’m trying to follow the book’s reasoning so far.

  • @string@fediverse.ro
    link
    fedilink
    11 year ago

    You are appending the result of the comparison value % 3 == 0 to the list multiples_of_3, which will always be either True or False. Instead, you should be appending value to the list if it is a multiple of 3. You can do this by changing the line multiples_of_3.append(number) to multiples_of_3.append(value) if the if statement number = value % 3 == 0 is true.