Help in college exercise

Asked

Viewed 224 times

-2

The statement is:

Create a program that receives as input the total amount of a debt (natural number greater than zero) and the maximum amount that the debtor can pay every month (natural number greater than zero).

The program must display the rest of the debt before and after each monthly payment until the debt expires.

Note: when the debt is less than the maximum that the debtor can pay, he will pay exactly as he owes, he will never pay a superior.

In the first line a natural value greater than zero indicating the value of debt; in the second row the maximum amount the debtor can pay for month (again a natural value greater than zero).

The amount of debt remaining before monthly payment and the amount of debt remaining after monthly payment, according to the format in examples. Repeat as long as the debt does not expire.

Entree:

150
50

Exit:

(antes) 150
(depois) 100
(antes) 100
(depois) 50
(antes) 50
(depois) 0

My code:

maximo = int(input())
minimo = int(input())
count = maximo
cont_1 = minimo
while maximo > minimo:
  maximo -= minimo
  print('(antes) {}'.format(maximo))
  print('(depois) {}'.format(cont_1))

I don’t know where I’m going wrong with that code.

  • Ask a more descriptive question to help other people with the same question

  • 1

    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.

2 answers

3

First start by giving the correct names for the variables. It may seem like a silly detail, but giving better names helps a lot when programming.

At no time was mentioned the minimum value, only the maximum amount to be paid and the value of the debt, so we have only:

divida = int(input())
maximo = int(input())

Exactly in this order, which is what the statement says.

Then in the loop you should check if the debt is not reset (ie if divida > 0).

And within the while you discount the value of the debt, paying attention to the case where it is less than or equal to the amount to be paid (because in this case it must be zeroed)

while divida > 0: # enquanto ainda deve algo
    print("(antes) {}".format(divida))
    if divida <= maximo: # zerar a dívida 
        divida = 0
    else: # descontar o valor máximo do valor da dívida 
        divida -= maximo
    print("(depois) {}".format(divida))

Note that the prints relating to "before" and "after" are made... before and after discounting the value :-)

See here the code running.

  • I thought I had to use an accountant to raise the print before the debt

  • @Danilobalbo No counter needed, since the only condition to continue the loop is that the debt value is greater than zero: "Repeat as long as the debt does not expire."

  • I’m very doubtful in that part of college while and for, doesn’t get in my head

  • The structures while and for sane bastante used by quaisquer programming languages. Therefore, I advise to study them well, not only their forms básicas, but also, as in the case of Python, their diversas variants. These variants are widely used in List Comprehesion.

1

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.

Browser other questions tagged

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