Looping ignores the rest of the program

Asked

Viewed 54 times

0

I was writing a program to calculate Paypal fees to help some friends of mine, but in my section against user faults, if the input is not specified, the looping simply ignores the rest of the program and closes. What’s wrong? EDIT: I also notice that Else is ignored

while True:
    response = input("Você quer calcular o quanto vai receber, ou o quanto precisa ser pago?\nResponda com recebido ou pago: \n")
    if response == 'pago' or 'recebido':
        break
    else:
        print("Algo deu errado. Responda especificamente com 'pago' ou 'recebido")
try:
    if response == "recebido":
        given = input("Ponha o quanto será pago: ")
        given = float(given)
        received = float(given * 0.9521) - 0.60
        print('O recebido será ',received,'.')
    elif response == "pago":
        received = input("Ponha o quanto quer receber: ")
        received = float(received)
        given = float(received + 0.60) / 0.9521
        print('Você precisará pagar ',given,'.')
except:
    print("Algo deu errado, tente outra vez. Lembre-se de usar pontos '.', e não vírgulas ','")
  • It wasn’t supposed to keep looping endlessly until it was right?

1 answer

0


Problem

You put a string in the second condition, so Python interprets it as a true result:

if response == 'pago' or 'recebido':

Solution

You must put the variable again to compare with the string "received".

if response == 'pago' or response == 'recebido':

Python interprets a non-empty string as a true result, as you used an "or" and the second condition was true it entered if and stopped while because of the break, and did not enter any more later if implying that the program was immediately closed.

Upshot

while True:
    response = input("Você quer calcular o quanto vai receber, ou o quanto precisa ser pago?\nResponda com recebido ou pago: \n")
    if response == 'pago' or response == 'recebido':
        break
    else:
        print("Algo deu errado. Responda especificamente com 'pago' ou 'recebido")
try:
    if response == "recebido":
        given = input("Ponha o quanto será pago: ")
        given = float(given)
        received = float(given * 0.9521) - 0.60
        print('O recebido será ',received,'.')
    elif response == "pago":
        received = input("Ponha o quanto quer receber: ")
        received = float(received)
        given = float(received + 0.60) / 0.9521
        print('Você precisará pagar ',given,'.')
except:
    print("Algo deu errado, tente outra vez. Lembre-se de usar pontos '.', e não vírgulas ','")

Execute

Browser other questions tagged

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