如何缩短' if,else'在python中的语句?

时间:2017-08-03 07:27:49

标签: python if-statement

我想缩短我的w == v声明,以及它的外观:

if, elif, else

以下是我的尝试:

transparency == False
if transparency == 'true':
    transparency = True
elif transparency == 'false':
    transparency = False
else:
    transparency = True

我认为它的工作方式类似于javascript简写,我错了吗?

2 个答案:

答案 0 :(得分:5)

你太复杂了。仅当False'false' 其他任何内容时,该值为True

transparency = transparency != 'false'

否则你的Javascript语法与Python混在一起; Python conditional expression syntax拼写

<true_expr> if <test> else <false_expr>

答案 1 :(得分:3)

基本上,如果transparency不是'false',那么它将是True。所以......

transparency = transparency != 'false'