I am new programming in Python. I am trying to create a function that simply takes the lowest payment (which starts at 10) and it simply returns the remaining balance (after 12 months of payments).

  1. Outside the function I am using a loop that calls the function and checks if the remaining balance is zero or less than zero. If it is not, increment the lowest payment +$10 and call the function again. When remaining balance is zero or less than zero print out the lowest payment.

In theory the output should be as follows :

***Test Case 1:***

balance = 3329
annualInterestRate = 0.2

Result Your Code Should Generate:
-------------------
******Lowest Payment: 310******


***Test Case 2:***

balance = 4773
annualInterestRate = 0.2

Result Your Code Should Generate:
-------------------
**Lowest Payment: 440**

***Test Case 3:***

balance = 3926
annualInterestRate = 0.2

Result Your Code Should Generate:
-------------------
**Lowest Payment: 360**

So far, this is what I have :

balance = 100
annualInterestRate = 0.2
per_month = ( annualInterestRate / 12 )
convert_to_str = str(per_month)[:4]
per_month = float(convert_to_str)
lowest_payment = 0

def main():
    i = 0
    while i < 11:
        global balance
        global lowest_payment
        global per_month
        balance = balance - lowest_payment
        balance = ((balance * per_month) + balance)

        i = i +1 

    #print (balance)

main()

if balance <= 0 or balance == 0:
    print "Lowest Payemnt: " + str(lowest_payment)
else:
    lowest_payment = lowest_payment + 10
    main()

The problem is that is is not executing my function, and go over the loop again.

Your help is greatly appreciated.

Edited 3 Years Ago by pyTony: Format fix

Hint ...

rate = 0.2
monthly_rate = rate/12

min_payment = 10

total = 0
for month in range(12):
    total += min_payment + min_payment*monthly_rate
    print("Month {:2d} total = {:.2f}".format(month+1, total))  # test

''' result ...
Month  1 total = 10.17
Month  2 total = 20.33
Month  3 total = 30.50
Month  4 total = 40.67
Month  5 total = 50.83
Month  6 total = 61.00
Month  7 total = 71.17
Month  8 total = 81.33
Month  9 total = 91.50
Month 10 total = 101.67
Month 11 total = 111.83
Month 12 total = 122.00
'''

Edited 3 Years Ago by vegaseat

Yes, agree with you , but that is causing to round up to the next number .

Ex.

rate = 0.2/12 = 0.0166666

when I use :

print '%.2f'% monthly_rate  

it is the same as

print str(round(rate, 2))

it rounds up to the next number 0.2 .

Why in my code, it is not going through the loop , or calling the function ? What am I doing wrong ?

You made me think .. and I think I finally found it ...

balance = 100
annualInterestRate = 0.2
month_payment = 10
month_interest = annualInterestRate/12

def debt(balance,month_payment,month_interest):
    for i in xrange(12):
        balance = (balance - month_payment)+month_interest*(balance - month_payment)
    return balance

final_bal = 0
i = 0
while final_bal >=0:
    final_bal = debt(balance,month_payment*i,month_interest)
    month_pay = i*month_payment
    i += 1
print 'Lowest Payment: '+str(month_pay)
This question has already been answered. Start a new discussion instead.