Python elegant assignment based on True/False values

后端 未结 11 1527
日久生厌
日久生厌 2021-01-31 09:32

I have a variable I want to set depending on the values in three booleans. The most straight-forward way is an if statement followed by a series of elifs:

if a a         


        
11条回答
  •  难免孤独
    2021-01-31 10:06

    What about nested ifs - it means you don't have to check everything several times and reads clearer to me (although maybe not quite as clever as some of the other answers):

    if a:
        if b:
            if c:
                name="first"
            else:
                name="second"
        else:
            if c:
                name="third"
            else:
                name="fourth"
    else:
        if b:
            if c:
                name="fifth"
            else:
                name="sixth"
        else:
            if c:
                name="seventh"
            else:
                name="eighth"
    

提交回复
热议问题