0

I'm testing this to count the number of even numbers vs the number of odd numbers in the first 3 elements of an array.

This works:

 for i in range(3):
    if int(numbers[i]) % 2 == 0:
        ev +=1
    else:
        od +=1
    print(ev, od)

But this gives me an error:

for i in range(3):
        ev += 1 if numbers[i] % 2 == 0 else od += 1
        print(ev, od)

ev += 1 if numbers[i] % 2 == 0 else od += 1
                                            ^
SyntaxError: invalid syntax

Any ideas?

0

1 Answer 1

1

Inline if/else is for values. So, in the else case, it's reading as:

ev += od += 1

That doesn't make sense. In this case, inline if/else, aka the ternary conditional, is not appropriate.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.