+ 1
Else, If, and Elif.
Why did Python give the option of Putting Else: If, when you could just use the Elif instruction? Is there something extra you could do with it? E.G If 1 + 1 = 2: print("One plus one is two.") Else: If 1+1 != 2: print("One plus one doesn't equal two.") Instead of-- If 1 + 1 = 2: print("One plus one is two.") Elif 1+1 != 2: print("One plus one doesn't equal two.") Many Thanks. ^^
3 Answers
+ 2
if you want to check multiple conditions and none of them are true, else is used printing "input was not correct" or something like that.
+ 2
x = 2
if x == 1:
doSomething()
elif x == 2:
doSomethingElse()
else:
print(x)
if x == 1:
doSomething()
else:
elif x == 2:
doSomethingElse()
print(x)
Those 2 examples are different. The second case allows you to decide for something to happen regardless of the secondary condition.
+ 1
else = not all .
elif = an other test .