mercredi 21 septembre 2016

else essentially superfluous

I don't have a background in programming, so this is probably really dumb, but I've never considered this before: it seems that the else statement is essentially superfluous because when the condition is False, Python just moves to the next unindented line .

For example, normally you would write: (if not using elif)

x=2
if x == 1: 
    value = "one"
else:
    if x == 2:
        value = "two"
print value

But this works too:

x=2
if x == 1: 
    value = "one"
if x == 2:
    value = "two"
print value

Could someone give an example that shows how and when the else: statement is essential?

Aucun commentaire:

Enregistrer un commentaire