else
takes no condition. It's just else:
, nothing more; the block is executed when the if
condition (and any elif
conditions) didn't match. Use elif
if you must have another condition to test on.
In your case, just use
if x == y:
print('x and y are equal')
elif x < y:
print('x is less than y')
else:
print('x is greater than y')
There is no need to explicitly test for x > y
, because that's the only option remaining (x
is not equal or less, ergo, it is greater), so else:
is fine here.
Note that I collapsed your nested if ... else
statement into an elif ... else
extension on the top-level if
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…