-2
I’m starting in programming and would like to know if I have how to decrease the uses of ifs
"""
Classification of a person by height and weight
alt = float(input("Digite a sua altura"))
p = float(input('Digite seu peso'))
if alt < 1.20 and p <= 60:
print(f'Classificação A')
elif alt < 1.20 and 60 < p < 90:
print(f'Classificação D')
elif alt < 1.20 and p > 90:
print(f'Classificação G')
elif 1.20 < alt < 1.70 and p <= 60:
print(f'Classificação B')
elif 1.20 < alt < 1.70 and 60 < p < 90:
print(f'Classificação E')
elif 1.20 < alt < 1.70 and p > 90:
print(f'Classificação H')
elif alt > 1.70 and p <= 60:
print('Classificação C')
elif alt > 1.70 and 60 < p < 90:
print('Classificação F')
elif alt > 1.70 and p > 90:
print('Classificação I')
For this code, I see no reason to decrease the
ifs
, but you can replace it withswitch
or by afunção (def)
– Codigo de Senior
I still do not intend the difficulty of copy and stick together the code... the person does the hardest and the least helps...
– Edu Mendonça
will not decrease but uses logic to optimize, look carefully and see that vc validates 3 times (alt < 1.20), 3 times (1.20 < alt < 1.70) and 3 times (alt > 1.70), the ideal is to decrease to only one validation
– Elton Nunes