1
if i+1 < stages[i]: tried[i] = (tried[i] + 1) if tried[i] != None else tried[i] = 0

if i+1 < stages[i]: tried[i] = (tried[i] + 1) if tried[i] != None else tried[i] = 0 ^ SyntaxError: can't assign to conditional expression

I got an error of cannot assign to conditional expression. Not sure which part I did wrong.

7
  • Why not a "normal" if else statement instead? Commented Aug 5, 2019 at 10:34
  • 3
    true_expr if cond else false_expr only works with expressions. Assignment is not an expression; use the full if statement, or extract the assignment outside the if expression. Commented Aug 5, 2019 at 10:34
  • ....can you try to format it properly...as there is probably the issue Commented Aug 5, 2019 at 10:34
  • 1
    @Amadan well obviously somehing is wrong, but fair enough, if the last statement just goes to 0 as in the answers below, it would work. Nevertheless, a line break after the first if statement would, e.g., help. The readability of mixing if and conditional is definitively not the best. Commented Aug 5, 2019 at 10:44
  • 1
    @Amadan :) agreed. Commented Aug 5, 2019 at 10:49

2 Answers 2

4

Should be:

if i+1 < stages[i]: tried[i] = (tried[i] + 1) if tried[i] != None else 0

without the last triend[i] = 0, just 0 (also triend would be probably wrong anyway).

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

1 Comment

Also, as an extra piece of advice: if you use -1 as initialization for tried[i], this could become a single if: tried[i] = (tried[i] + 1) if i + 1 < stages[i] else -1. Moreover, if you can initialize tried[i] to -1 beforehand, you could simply write: tried[i] += int(i + 1 < stages[i]).
1

The overall structure you're looking for, since you are trying to accomplish an assignment with multiple conditional expressions, is this: variable = expression condition else expression condition ... else expression. You can even use parentheses to remind yourself of how it will be parsed: variable = (expression condition else expression condition ... else expression). The total conditional expression must end with "else".

For instance, I have the following code:

x = 5
if x == 1:
    a = '1'
elif x == 2:
    a = '2'
else:
    a = '3'
print(a)

Using a conditional expression, it becomes:

x = 5
a = '1' if x == 1 else '2' if x == 2 else '3'
print(a)

I'm not sure what your assignment and conditional expressions

if i+1 < stages[i]: tried[i] = (tried[i] + 1) if tried[i] != None else tried[i] = 0

would look like when expanded, as some information is missing:

missing_expression = '?'
if i+1 < stages[i]:
    tried[i] = (tried[i] + 1)
elif tried[i] != None:
    tried[i] = missing_expression
else:
    tried[i] = 0

When the missing expression is supplied, your conditional expression should look like:

tried[i] = (tried[i] + 1) if i+1 < stages[i] else missing_expression if tried[i] != None else 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.