8

Code:

for i in range(1000):
    print(i) if i%10==0 else pass

Error:

File "<ipython-input-117-6f18883a9539>", line 2
    print(i) if i%10==0 else pass
                            ^
SyntaxError: invalid syntax

Why isn't 'pass' working here?

Mysterious
  • 525
  • 1
  • 5
  • 18

2 Answers2

13

This is not a good way of doing this, if you see this problem the structure of your code might not be good for your desires, but this will helps you:

 print(i) if i%10==0 else None
Mehrdad Pedramfar
  • 9,313
  • 3
  • 31
  • 54
1

This is not a direct answer to your question, but I would like suggest a different approach.

First pick the elements you want to print, then print them. Thus you'll not need empty branching.

your_list = [i for i in range(100) if i%10]
# or filter(lambda e: e%10 == 0, range(100))
for number in your_list:
    print number
marmeladze
  • 5,543
  • 3
  • 21
  • 41