-3

What I have so far:

num = range(0, 101, 3)
list = []

if num % 3 == 0:
    list.append

print(list)
DavidG
  • 24,279
  • 14
  • 89
  • 82
pythonnewbie
  • 1
  • 1
  • 4
  • 3
    Hi, welcome to SO! There are some issues with the way you asked your question which will make it difficult for folks in the community to answer you. It would be helpful if you: (1) write an explicit question into the body of your question (like "I get the output "blah blah" but I am expecting "foo foo". What error am I making in my code" (2) format your code using the brace button in the edit box (3) give some description in your text or comments of what the code is doing now, so we can follow your thinking. – mtrw Oct 31 '16 at 16:02
  • expected input/output >>> 97 % 10 7 – pythonnewbie Oct 31 '16 at 16:05
  • 1
    Also, just as a hint, if a number if a multiple of both 3 and 2, it's necessarily a multiple of 6... – mtrw Oct 31 '16 at 16:07

2 Answers2

5

I think this is what you are trying to do:

print("\n".join(str(i) for i in range(0, 101, 3) if i % 2 == 0))

or

print([i for i in range(0, 101, 3) if i % 2 == 0])

I am using a list comprehension here.

print(list(range(0, 101, 6)))

does the same thing however.

CodenameLambda
  • 1,486
  • 11
  • 23
1
lst = []   # don't use Python inbuilt names for variables

for num in range(0,101,3):
    if num % 2 == 0:              # you already go through the numbers in steps of 3
        lst.append(num)

print(lst)
Ukimiku
  • 608
  • 1
  • 6
  • 13
  • 1
    `list` isn't a keyword, it is a name of in `__builtins__`. (If you call `globals()` it has an entry called `__builtins__` that contains all these classes and functions) But you are right in stating that it isn't a good style to shadow those names. – CodenameLambda Oct 31 '16 at 16:18
  • Thanks. I'll clarify. – Ukimiku Oct 31 '16 at 16:37