number of repeated elements of an array

Asked

Viewed 31 times

-1

compra = int(input('Valor da compra: R$'))
valorPago = int(input('Valor pago: R$'))
troco = valorPago - compra
dimDisponivel = [100, 50, 25, 10, 5, 2, 1, 0.50, 0.25, 0.10, 0.05, 0.01]
print(f'Troco: R${troco}\n')



for i in dimDisponivel:
    while troco >= i:
        print(f'{dimDisponivel.count(i)} de R${i}')
        troco -= i

I would like to know how to show only once the amount of a certain repeated banknote, because if the program detects 3 banknotes of 2 reais, it writes 1 of each per line, and I would like to know how to put the 3 notes of 2 in one line.

  • Although the above questions have titles that do not seem to have anything to do with your case, they brought the same problem. Just adapt the solutions to your case

1 answer

0

Your Count() method is counting the number of values in the list, not the ones needed to complete the change. Make a while counter for that.

for i in dimDisponivel:
    count = 0
    while troco >= i:
        count += 1
        troco -= i
    if count != 0:
        print(f'{count} de R$ {i}')

Browser other questions tagged

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