0
Hello, I am trying to print values with different decimal accuracies, but even after attempts and researches, I could not find a solution. The code snippet with this problem is:
valor_monetario = round(float(input()), 2)
moedas = [1, 0.50, 0.25, 0.10, 0.05, 0.01]
for valor in moedas:
qtde = int(valor_monetario / valor)
valor_monetario = valor_monetario % valor
print('{0} moeda(s) de R$ {0:.2f}'.format(round(qtde), round(valor, 2)))
the output for monetary value = 576.73 is this:
1 currency(s) of R$ 1.00
1 currency(s) of R$ 1.00
0 currency(s) of R$ 0.00
2 currency(s) of R$ 2.00
0 currency(s) of R$ 0.00
3 currency(s) of R$ 3.00
but it should be:
1 currency(s) of R$ 1.00
1 currency(s) of R$ 0.50
0 currency(s) of R$ 0.25
2 currency(s) of R$ 0.10
0 currency(s) of R$ 0.05
3 currency(s) of R$ 0.01
0.50, for example, is rounded to 1.00. If I simply print the isolated value in the form below (which is exactly as it is in the print above, but in isolation), it displays the value with the correct accuracy:
print('{0:.2f}'.format(round(valor, 2)))
How can I print in a single print, values with different accuracies? Thank you.
what is the monetary value variable?
– Lucas
Hi, it is an input value. The output shown has the monetary value with the value 576.73.
– Franklin Sousa
I split it with //, but it came out the same @Eltonnunes.
– Franklin Sousa
truth I spoke too fast, if you have a while you can take a look at the second half of this video, https://www.youtube.com/watch?v=prQ0TGPFFLk&t=2s
– Elton Nunes
No problem. I’ll see. Thanks, @Eltonnunes. Ah the part of the division you suggested does not interfere with the part where you are giving problem in the display of value.
– Franklin Sousa
your mistake is in the second couchetes, you’re passing zero, should be a
– Elton Nunes
@Eltonnunes , thanks for the help, but the error is not at that point you pointed out. If I pass 1 instead of zero, the amount of coins will appear with a decimal place, but the amount should not have decimal place after the comma. If I do this, the output will look like this:
1 moeda(s) de R$ 1.00 
0.5 moeda(s) de R$ 1.00 
0.25 moeda(s) de R$ 0.00 
0.1 moeda(s) de R$ 2.00 
0.05 moeda(s) de R$ 0.00 
0.01 moeda(s) de R$ 3.00 

ps.: I’m about halfway through your video.– Franklin Sousa
vc tried the other couchete? print('{0} currency(s) of R$ {1:. 2f}'. format(round(Qtde), value))
– Elton Nunes
@Eltonnunes, the solution you put in the video came out right. Then I saw that this last form that you commented also works. So these two shapes work (I need to better understand how this format() issue works:
print('{0} moeda(s) de R$ {1:.2f}'.format(qtde, valor))
print('{} moeda(s) de R$ {:.2f}'.format(qtde, valor))
Thank you very much for the help.– Franklin Sousa
cool that worked
– Elton Nunes