1
So I was doing some exercises in python, a language in which I’m learning to program, and it seems to me that the execution flow is not following correctly.
# Converte distancia
def mettocent(met):
cent = met * 100
return cent
def centtomet(cent):
met = cent / 100
return met
def mettokil(met):
kil = met / 1000
return kil
def kiltomet(kil):
met= kil * 1000
return met
def centtokil(cent):
kil = cent / 100000
return kil
def kiltocent(kil):
cent = kil * 100000
return cent
escolha = True
while escolha == True:
print('Escolha uma opção de conversão:')
print('1 Metros para centimetros\n2 Centimetros para metros\n3 Metros para quilometros')
print('4 Quilometros para metros\n5 Centimetros para quilometros\n6 Quilometros para centimetros\n')
escolha = int(input())
if escolha > 6:
print("Opção inválida, digite novamente")
continue
dist = float(input("Digite a distância a ser convertida:"))
if escolha == 1:
print(mettocent(dist), "centimentros")
break
elif escolha == 2:
print(centtomet(dist), "metros")
break
elif escolha == 3:
print(mettokil(dist), "quilometros")
break
elif escolha == 4:
print(kiltomet(dist), "metros")
break
elif escolha == 5:
print(centtokil(dist), "quilometros")
break
elif escolha == 6:
print(kiltocent(dist), "centimentros")
break
What happens is that when inside the repetition, he verifies that the value of the choice is greater than 6, and as far as I know, in python, everything other than 0 is True, does not go back to the beginning of the repetition, but comes out of it, giving it output, for example:
Escolha uma opção de conversão:
1 Metros para centimetros
2 Centimetros para metros
3 Metros para quilometros
4 Quilometros para Metros
5 Centimetros para quilometros
6 Quilometros para centimetros
7
Opção inválida, digite novamente
Process finished with exit code 0
(I know that there may be better ways to do this program, and that functions are not necessary, but adapting what I have learned so far in an exercise that only asked to "convert meters to centimeters")
I was able to get around the problem by assigning True to 'choice' if it fell into 'choice > 6' But I still wanted to understand why of the problem
– Neo son