else
/ elif
used after break
PYL-R172367
68while True:
69 elemento = input("Escolha o elemento \ncaso queira parar digite PARE\n")
70 if elemento == "PARE":71 break
72 else:
73 for i in Dic_eletro:
160 print("elemento ",Dicionario_para_lista[index_tupla][0])
161 valor = input("digite o valor a ser modificado no elemento caso queria sair apenas \npressione enter ")
162
163 if valor == '':164 break
165
166 else:
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