error in a real-to-dollar python converter exercise

Asked

Viewed 204 times

1

Hello, I’m trying to make a converter from real to dollar and to normal values it works but is giving error when I put the dollar*N10, type 420 real, or 840, it is left the exact value of the example dollar :

Type how many reals you have: 840

Enter the current quote of the dollar: 4.20

You with 840.00 real can buy 199.00 dollars and you have 4.20 real

Process finished with Exit code 0

I believe it’s because I’m using float but I don’t know how else to do it, follow the code below :

r = float(input('Digite quantos reais você tem: '))

d = float(input('Digite a cotação atual do dolar: '))

print('Você com {:.2f} reais pode comprar {:.2f} dólares e te sobram {:.2f} reais'.format(r, r // d, r % d))
  • It only makes sense to use the rest operator of the division for integer operands. You used for float operands.

1 answer

1


The problem is not the numerical conversion, but the fact that you are calculating the leftover as the rest of the division of real by dollar. The correct procedure is to subtract from the real income the value of the dollar converted by the exchange rate. See:

r = float(input('Digite quantos reais você tem: '))

d = float(input('Digite a cotação atual do dolar: '))

poder_compra = r//d
restante = r - (poder_compra*d)

print('Você com {:.2f} reais pode comprar {:.2f} dólares e te sobram {:.2f} reais'.format(r, poder_compra, restante))
  • Had the problem of all accounts give 0 rest, but I transformed poder_compra in int and everything worked out

Browser other questions tagged

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