2

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

I thought it'd work like the javascript shorthand, am I wrong?

0

2 Answers 2

5

You are overcomplicating things. 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>
Sign up to request clarification or add additional context in comments.

1 Comment

I knew I was overcomplicating things that's why I was wondering how I could do it in one line :)
3

Basically, if transparency is anything other than 'false', it's going to be True. So…

transparency = transparency != 'false'

3 Comments

"'id like to shorten my if, else if, else statement," You broke his concept by assuming his logic.
This is the logic OP's code expresses. The result is exactly the same.
@RoyHolzem The answer is a one liner that works in all context, it does respect my logic.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.