Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
February 21st, 2013, 02:05 PM
 upendra_35
Registered User

Join Date: Feb 2013
Posts: 2
Time spent in forums: 10 m 58 sec
Reputation Power: 0
Python newbie

Hi, I am very new to python and i currently had to do an assignment and eventhough i could do most of it but got struck in the middle. I hope i could get some help you finish my assignment. Thanks

Basically i have to write a python program to generate the below:

PHP Code:
```  balance = 4213               annualInterestRate = 0.2               monthlyPaymentRate = 0.04                              Result Your Code Should Generate:               -------------------               Month: 1               Minimum monthly payment: 168.52               Remaining balance: 4111.89               Month: 2               Minimum monthly payment: 164.48               Remaining balance: 4013.2               Month: 3               Minimum monthly payment: 160.53               Remaining balance: 3916.89               Month: 4               Minimum monthly payment: 156.68  ```

Here is my code for that:
PHP Code:
```  b0 = 4213 print('balance =' + str(b0)) r = 0.2 print('Annual interest rate =' + str(r)) m = 0.04 print('montly payment rate =' + str(m)) Month = 1 while Month <= 4:       print('Month:' + str(Month))     mmp = round(m * b0, 2)     print('Minimum monthly payment:' + str(mmp))     ub0 = b0 - mmp     rb = round(ub0 + (r/12.0 * ub0), 2)      print('Remaining balance:' + str(rb))     Month = Month + 1         print('Month:' + str(Month))       mmp = round(m * rb, 2)     print('Minimum monthly payment:' + str(mmp))     ub0 = rb - mmp     rb = round(ub0 + (r/12 * ub0), 2)     print('Remaining balance:' + str(rb))     Month = Month + 1  ```

When i run the above i am getting the following:

PHP Code:
```  balance =4213 Annual interest rate =0.2 montly payment rate =0.04 Month:1 Minimum monthly payment:168.52 Remaining balance:4111.89 Month:2 Minimum monthly payment:164.48 Remaining balance:4013.2 Month:3 Minimum monthly payment:168.52 Remaining balance:4111.89 Month:4 Minimum monthly payment:164.48 Remaining balance:4013.2 Total paid:199.8 Remaining balance:4013.2  ```

As you can see i got only two months correct but after that the last two months have the same values as the first two months. Could somebody point to me where i am going wrong.

Thanks
Upendra

#2
February 22nd, 2013, 12:58 AM
 dwblas
Contributing User

Join Date: May 2009
Posts: 412
Time spent in forums: 5 Days 5 h 27 m 28 sec
Reputation Power: 32
You always subtract the payment from the original balance (b0) on each pass through the loop
Code:
```b0 = 4213
print('balance =' + str(b0))
r = 0.2
print('Annual interest rate =' + str(r))
m = 0.04
print('montly payment rate =' + str(m))
min_payment=round(m * b0, 2)
remaining_balance=b0
Month = 1
while Month <= 4:
print('\nMonth:' + str(Month))
print('Minimum monthly payment:', min_payment)
remaining_balance -= min_payment
print('Remaining balance:', remaining_balance)
Month = Month + 1     ```

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Python newbie