Syntax error with ternary operator

前端 未结 1 1177
-上瘾入骨i
-上瘾入骨i 2021-01-20 04:00

I\'m new to Python and I\'m trying to use ternary opertor which has this format (I think so)

value_true if  else value_false

He

1条回答
  •  粉色の甜心
    2021-01-20 04:38

    Ternary operation in python using for expression, not statements. Expression is something that has value.

    Example:

    result = foo() if condition else (2 + 4)
    #        ^^^^^                   ^^^^^^^
    #      expression               expression
    

    For statements (code blocks such as continue, for, etc) use if:

    if condition:
         ...do something...
    else:
         ...do something else...
    

    What you want to do:

    expanded = set()
    
    while not someExpression:
        if currentState not in expanded: # you use set, so this condition is not really need
             expanded.add(currentState)
             # some code here
    

    0 讨论(0)
提交回复
热议问题