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

Asked

Viewed 275 times

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

1 answer

3


and as far as I know, in python, everything other than 0 is true

Your premise is wrong.

In Python, any non-zero number will be considered a true value, but not necessarily equal to True. Are the so-called Truthy values, if you want to research 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 a specialization of the integer type, where the value True is equal to int(1) and the False is equal to int(0). So in doing True == 1, the result will be true, but True == 6 will be false.

To get around this, you can trade while escolha == true for while true only.

Other details on the implementation of this problem I have already commented on:

  • Valeuu! Well my teacher doesn’t explain things completely rs

  • 3

    @Nelsonp your best teachers are here, hehehe

Browser other questions tagged

You are not signed in. Login or sign up in order to post.