In this issue you have to pay attention in two situations: 1º verify the resto da divisão
between the debt and the maximum value of the tranche and 2º verify the total
of plots.
Note that, in some situations, the rest of the division between the debt and the maximum amount of the installment será igual a "0"
, which means that all plots will have the same value.
Example 1:
divida = 100
valor_maximo_parcela = 10
In this situation all plots will have the value of 10
.
In other situations, the rest of the division between debt and maximum tranche value will be diferente de "0"
. This means that the last instalment will have value between "0"
and the value "maximo_parcela"
.
Example 2:
divida = 100
valor_maximo_parcela = 32
In this situation the value of the last tranche will be different from the previous
Faced with this situation we can say that to pay a debt of 100, with the maximum value of each installment equal to 32, it would be necessary 4
plots. For the division held here would not be the actual division "/"
and yes the whole division "//"
. In this case, we would have three parcels of 32 and a parcel (the last) of 4.
Faced with such observations already addressed, I developed the following algorithm.
# Capturando e tratando o valor da dívida:
while True:
try:
divida = int(input('Valor total da dívida: '))
if divida <= 0:
print('\033[31mValor INVÁLIDO! Digite apenas valores maiores que "0"!\033[m')
else:
break
except:
print('\033[31mValor INVÁLIDO! Digite apenas números inteiros!\033[m')
# Capturando e tratando o maior valor por parcela mensal:
while True:
try:
valor_parcela = int(input('Maior valor por parcela mensal: '))
if valor_parcela <= 0:
print('\033[31mValor INVÁLIDO! Digite apenas valores maiores que "0"!\033[m')
else:
break
except:
print('\033[31mValor INVÁLIDO! Digite apenas números inteiros!\033[m')
# Realizando os cálculos:
resto = divida % valor_parcela
if resto == 0:
parcela = (divida // valor_parcela)
else:
parcela = ((divida // valor_parcela) + 1)
# Realizando cálculos finais e exibindo resultados:
print()
ordem = 0
while divida > 0:
ordem += 1
print(f'\033[32mDívida antes do pagamento da {ordem}º parcela: {divida}')
divida = (divida - valor_parcela)
if divida >= parcela:
print(f'Dívida após o pagamento da {ordem}ª parcela é: {divida}')
elif 0 < divida < parcela:
print(f'Dívia após o pagamento da {ordem}ª parcela é: {divida}')
print(f'Dívida após o pagamento da última parcela é: 0\033[m')
See how the algorithm works on Repl.it
Note that this algorithm also performs a treatment of the values received by the inputs and only lets progress if the values are integer and larger than zero.
Ask a more descriptive question to help other people with the same question
– Afonso
Danilo, when an answer solves your problem and there is no doubt left, consider marking it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. I’ve been looking at your question history and they all have answers and they’re still open.
– Augusto Vasques