else
/ elif
used after break
PYL-R1723That’s all we can say right now.
The use of else
or elif
becomes redundant and can be dropped if the last statement under the leading if
/ elif
block is a break
statement.
In the case of an elif
after break
, it can be written as a separate if
block.
For else
blocks after break
, the statements can be shifted out of else
. Please refer to the examples below for reference.
Refactoring the code this way can improve code-readability and make it easier to maintain.
def changing_denominators(p, q):
while True:
if q == 0:
break
else:
print(p/q)
q = q - 1
def changing_denominators(p, q):
while True:
if q == 0:
break
print(p/q)
q = q - 1