mardi 24 mars 2020

using multiple if else statements in one line

Is is possible to use multiple if else statements in one line?

I know just a pair of if else can be written as below:

weight = 50

if score > 60:
    weight = 'heavy'
else:
    weight = 'light'

print(weight)
weight = 50
weight = 'heavy' if weight > 60 else 'light'
print(weight)

If I write two pair of if else, or use if else else else elif, how can I write that into one sentence?

weight = 50
height = 165

if score > 60:
    if height > 170 = 'skinny'
        else : 'fat'
else:
    if height > 160 = 'skinny'
        else : 'fat'    
print(weight)

I know it is not recommended but just want to know.

Thank you

Aucun commentaire:

Enregistrer un commentaire