0
a = 0
dots = []
while True:
    a = c.find('.', a+1)
    print(a)
    dots.append(a) if (a != -1) else break

Why does this return an invalid syntax at the break?

1 Answers1

0

You cannot use the conditional operator that way.

You could write something like:

is_even = True if x%2==0 else False
# or
result = foo() if should_call_foo else None

when you're deciding which of two options to assign to a variable.

But break is a control statement and can't go here.

You probably want something using the traditional if/else approach, for example:

a = 0
dots = []
while True:
    a = c.find('.', a+1)
    print(a)
    if (a != -1):
        dots.append(a)
    else:
        break
chepner
  • 497,756
  • 71
  • 530
  • 681
jedwards
  • 29,432
  • 3
  • 65
  • 92