I'd like to shorten my if, elif, else
statement, Here's what it looks :
transparency == False
if transparency == 'true':
transparency = True
elif transparency == 'false':
transparency = False
else:
transparency = True
And here's what I tried :
transparency == False
transparency == 'true' ? True: False #boolean type
我以为它会像javascript速记一样工作,我错了吗?
最佳答案
你太复杂了。The value is False
only if originally equal to 'false'
, True
for anything else:
transparency = transparency != 'false'
You otherwise got your Javascript syntax mixed up with Python; the Python conditional expression syntax is spelled
<true_expr> if <test> else <false_expr>