Problems with the 'continue' or 'while' command in Python

1
So I was doing some exercises in python, the 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 repeat, it checks that the value of the choice is greater than 6, and as far as I know, in python, anything other than 0 is True, does not return to the beginning of the repeat, but exit 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 there may be better ways to do this program, and that functions are not necessary, but I'm adapting what I've learned so far in an exercise that just asked me to "convert meters to centimeters")

    
asked by anonymous 21.09.2018 / 21:27

1 answer

3
  

And as far as I know, in python, anything other than 0 is True

Your premise is wrong.

In Python, any nonzero number will be considered a true value, but not necessarily equal to True . These are the truthy values , if you want to search more about.

if 6:
    print('Ok')  # Exibe, pois 6 é um truthy value

However, if you do:

if 6 == True:
    print('Ok')  # Não exibe, pois 6 não é igual a True

Even in Python, the Boolean type is an integer-type specialization, where True is equal to int(1) and False equals int(0) . So, by doing True == 1 , the result will be true, but True == 6 will be false.

To get around this, you can change while escolha == true to while true only.

Further details on the implementation of this issue I've already commented on:

21.09.2018 / 21:42