dimanche 19 septembre 2021

Python indentation syntax is not reliable, why?

Pythoners! (think more before giving down!)

I don't like Python syntax because of the following example. Can someone explain if there is a logical solution for this situation? Two nested if's are not identical, although the conversion from if...else... if... to if...elif is done correctly. A reason why indentation is not reliable.

if (5 > 40):
    print("A")
    print("B")
else:
    if (4 > 30):
        print("C")
    else:
        print("D")
    print("E")
print("F")

and

if (5 > 40):
    print("A")
    print("B")
elif (4 > 30):
    print("C")
else:
    print("D")
print("E")
print("F")

Change 5 to 50 and compare the outputs.

If it was C-Python language, we could enforce the logic by braces:

if (5 > 40):
    print("A")
    print("B")
elif (4 > 30): {
    print("C")
else:
    print("D")
print("E")
}
print("F")

Aucun commentaire:

Enregistrer un commentaire