lundi 11 mai 2020

How to put nested if conditions in single line

For a situation like this:

a = int(input())

if a > 6:
    right()
elif:
    left()

def right():
    print('great')

def left():
    print('less')

We can make the condition part a single line like this:

a = int(input())

left() if a > 6 else right()

def right():
    print('great')

def left():
    print('less')

Then what about nested if conditions like:

a = int(input())

if a > 0 and a < 12:
    if a > 6:
        right()
    else:
        left()
else:
    print('wrong')

How to put the above like code in single line (or any other simpler way)?

Aucun commentaire:

Enregistrer un commentaire