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

#1
November 28th, 2012, 04:18 AM
 TheNightArrow
Registered User

Join Date: Nov 2012
Posts: 3
Time spent in forums: 4 h 2 m 46 sec
Reputation Power: 0
My 1st time trying to write a program

I plan to move into something to make games at a later date, but have been told I should learn the basics before messing with harder stuff and that if I have the basic’s learned I can pick up other languages much easier so I decided this class should work fine for now, right?

Ok so I am following along this YouTube MIT class on coding with Python. ( I am not allowed to post the link to the class apparently :-\)

I just finished watching lecture 3 and on the little assignment thing that comes with it, assignment one should be do-able because its " due " on lecture 4. basically its is about paying off a credit card debt for the time of one year.

(problem copy/pasted from the PDF)

Paying the Minimum

Problem 1

Write a program to calculate the credit card balance after one year if a person only pays the minimum monthly payment required by the credit card company each month.

Use raw_input() to ask for the following three floating point numbers:
1. the outstanding balance on the credit card
2. annual interest rate
3. minimum monthly payment rate

For each month, print the minimum monthly payment, remaining balance, principle paid in the format shown in the test cases below. All numbers should be rounded to the nearest penny. Finally, print the result, which should include the total amount paid that year and the remaining balance.

Test Case 1

>>>
Enter the outstanding balance on your credit card: 4800
Enter the annual credit card interest rate as a decimal: .2
Enter the minimum monthly payment rate as a decimal: .02

Month: 1
Minimum monthly payment: \$96.0
Principle paid: \$16.0
Remaining balance: \$4784.0
Month: 2
Minimum monthly payment: \$95.68
Principle paid: \$15.95
Remaining balance: \$4768.05
Month: 3
Minimum monthly payment: \$95.36
Principle paid: \$15.89
Remaining balance: \$4752.16
Month: 4
Minimum monthly payment: \$95.04
Principle paid: \$15.84
Remaining balance: \$4736.32
Month: 5
Minimum monthly payment: \$94.73
Principle paid: \$15.79
Remaining balance: \$4720.53
Month: 6
Minimum monthly payment: \$94.41
Principle paid: \$15.73
Remaining balance: \$4704.8
Month: 7
Minimum monthly payment: \$94.1
Principle paid: \$15.69
Remaining balance: \$4689.11
Month: 8
Minimum monthly payment: \$93.78
Principle paid: \$15.63
Remaining balance: \$4673.48
Month: 9
Minimum monthly payment: \$93.47
Principle paid: \$15.58
Remaining balance: \$4657.9
Month: 10
Minimum monthly payment: \$93.16
Principle paid: \$15.53
Remaining balance: \$4642.37
Month: 11
Minimum monthly payment: \$92.85
Principle paid: \$15.48
Remaining balance: \$4626.89
Month: 12
Minimum monthly payment: \$92.54
Principle paid: \$15.43
Remaining balance: \$4611.46
RESULT
Total amount paid: \$1131.12
Remaining balance: \$4611.46

ok so that is, what I assume, I should see when I run the program in Python.

Here the what I have typed so far:

x = float (raw_input (‘1. What is the outstanding balance on the credit card?:’))
y = float (raw_input (‘2. What is the annual intrest rate?:’))
z = float (raw_input (‘3. What is the minimum monthly payment rate?:’))
print ‘Month 1’
print ‘Minimum monthy payment: \$’,(x*z)
a = (y/12.0*x)
b = (x*z)
print ‘Principal paid: \$’, (b-a)
print ‘Remaining balance: \$’, (x-(b-a))

My problems so far are:
1. I feel like I am doing this the hard way having so many letters = things. I know that my code does work for month 1 tho! lol
2. I don’t know how to continue down this path for the next few months. In month 2, I can no longer use x because my total balance will now be smaller.
3. I feel like I should be using " if ", " else ", and " while " statements in my code but not really sure how they fit in here.

I am not looking for the flat out answer, there is an answer key that comes with the assignments so if I am really 100% stuck I can open it up and see how it is coded. But I am trying to grasp the idea of it without just reading the code they used, I needed to be able to write my own and understand why it makes since. Any info would be appreciated. Thanks in advance!

#2
November 28th, 2012, 06:38 AM
 TheNightArrow
Registered User

Join Date: Nov 2012
Posts: 3
Time spent in forums: 4 h 2 m 46 sec
Reputation Power: 0
update to where I am currently at

Current code:

balance = float (raw_input (‘1. What is the outstanding balance on the credit card?:’))
interest = float (raw_input (‘2. What is the annual interest rate?:’))
minipercent = float (raw_input (‘3. What is the minimum monthly payment rate?:’))
principal = (interest/12.0*balance)
minipay = (balance*minipercent)
print ‘Month 1’
print ‘Minimum monthy payment: \$’,(balance*minipercent)
print ‘Principal paid: \$’, (minipay-principal)
print ‘Remaining balance: \$’, (balance-(minipay-principal))
balance = balance – 16.0
principal = (interest/12.0*balance)
minipay = balance*minipercent
print ‘Month 2’
print ‘Minimum monthly payment: \$’,round((balance*minipercent),2)
print ‘Principal paid: \$’,round((minipay-principal),2)
print ‘Remaining balance: \$’,round((balance-(minipay-principal)),2)
balance = balance – 15.95
principal =(interest/12.0*balance)
minipay = balance*minipercent
print ‘Month 3’
print ‘Minimum monthly payment: \$’,round((balance*minipercent),2)
print ‘Principal paid: \$’,round((minipay-principal),2)
print ‘Remaining balance: \$’,round((balance-(minipay-principal)),2)
balance = balance – 15.89
principal = (interest/12.0*balance)
minipay = balance*minipercent
print ‘Month 4’
print ‘Minimum monthly payment: \$’,round((balance*minipercent),2)
print ‘Principal paid: \$’,round((minipay-principal),2)
print ‘Remaining balance: \$’,round((balance-(minipay-principal)),2)
balance = balance – 15.84
principal = (interest/12.0*balance)
minipay = balance*minipercent
print ‘Month 5’
print ‘Minimum monthly payment: \$’,round((balance*minipercent),2)
print ‘Principal paid: \$’,round((minipay-principal),2)
print ‘Remaining balance: \$’,round((balance-(minipay-principal)),2)

There has got to be a way for me to repeat without me doing a copy/paste on:
balance = balance - (x)
principal = (interest/12.0*balance)
minipay = balance*minipercent
print 'Month (y)'
print 'Minimum monthly payment: \$',(balance*minipercent)
print 'Principal paid: \$', (minipay-principal)
print 'Remaining balance: \$', (balance-(minipay-principal))

( my issues with doing that copy/paste is I have to run the program for every month to see what my new (x) value is then plug it in. )

Last edited by TheNightArrow : November 28th, 2012 at 08:26 AM. Reason: Learned to round

#3
November 28th, 2012, 10:25 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 3,458
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
Sorry, I tend to answer with code. Comments mark the changes to your code. I inserted a loop and inserted a condition that the minimum monthly payment is no less than 12 bucks or the remaining balance. For you lazy readers, the message is "don't carry high interest loans".
Quote:
 You paid 25802.08 on a 4800.00 balance over 56 years
Code:
```x = float(raw_input ('1. What is the outstanding balance on the credit card?:'))
y = float(raw_input ('2. What is the annual interest rate?:'))
z = float(raw_input ('3. What is the minimum monthly payment rate?:'))
month = 0
original_debt = x
total_paid = 0
minimum_monthly_payment = 12
while 0 < x:                               # repeat until debt is paid
month += 1                             # increment the month

#compute first
b = max((x*z),minimum_monthly_payment) # otherwise this could take forever.
b = min(b,x)
a = (minimum_monthly_payment < b) * (x * y/12.0)
p = b-a
r = x-p
total_paid += b
print 'Month',month       # Wow!  Displayed information can change
# string formatting eliminates fractional penny display
print 'Minimum monthy payment: \$%.2f'%b
print 'Principal paid: \$%.2f'% p
print 'Remaining balance: \$%.2f'% r
x = r                                 # update x

print('You paid %.2f on a %.2f balance over %d years'%(
total_paid,original_debt,round(month/12.0)))
print('Try to recover your gold watch from pawn shop.')```
__________________
[code]Code tags[/code] are essential for python code!

#4
November 29th, 2012, 08:10 AM
 TheNightArrow
Registered User

Join Date: Nov 2012
Posts: 3
Time spent in forums: 4 h 2 m 46 sec
Reputation Power: 0
Almost there

Ok so I feel like I’m almost there! My numbers are just coming out a little bit off so I’ll give my code, my results, and then what I should have. You will see my numbers are just a little bit off, biggest problem being my " total amount paid " line. The rest are just off by a few cents. so rounding errors maybe?? I’m sure anyone who is able to help me with this knows that some of those lines are indented lines but since I’m not sure how to make it look that way on this forum I marked them with " #indented line " just to make sure it was clear.

Current Code:

balance = float (raw_input (‘1. What is the outstanding balance on the credit card?:’))
interest = float (raw_input (‘2. What is the annual interest rate?:’))
minipercent = float (raw_input (‘3. What is the minimum monthly payment rate?:’))
principal = (interest/12.0*balance)
minipay = (balance*minipercent)
paid = 0
month = 0
while month < 12:
month = month + 1 #indented line
print ‘Month’, month #indented line
print ‘Minimum monthy payment: \$’,round((balance*minipercent),2) #indented line
print ‘Principal paid: \$’,round((minipay-principal),2) #indented line
print ‘Remaining balance: \$’,round((balance-(minipay-principal)),2) #indented line
balance = balance -(minipay-principal) #indented line
principal = (interest/12.0*balance) #indented line
minipay = balance*minipercent #indented line
paid = paid + (balance*minipercent) #indented line
print ‘Results’
print ‘Total amount paid:’, round((paid),2)
print ‘Remaining balance’, round((balance),2)

Results:

1. What is the outstanding balance on the credit card?:4800
2. What is the annual interest rate?:.2
3. What is the minimum monthly payment rate?:.02
Month 1
Minimum monthy payment: \$ 96.0
Principal paid: \$ 16.0
Remaining balance: \$ 4784.0
Month 2
Minimum monthy payment: \$ 95.68
Principal paid: \$ 15.95
Remaining balance: \$ 4768.05
Month 3
Minimum monthy payment: \$ 95.36
Principal paid: \$ 15.89
Remaining balance: \$ 4752.16
Month 4
Minimum monthy payment: \$ 95.04
Principal paid: \$ 15.84
Remaining balance: \$ 4736.32
Month 5
Minimum monthy payment: \$ 94.73
Principal paid: \$ 15.79
Remaining balance: \$ 4720.53
Month 6
Minimum monthy payment: \$ 94.41
Principal paid: \$ 15.74
Remaining balance: \$ 4704.79
Month 7
Minimum monthy payment: \$ 94.1
Principal paid: \$ 15.68
Remaining balance: \$ 4689.11
Month 8
Minimum monthy payment: \$ 93.78
Principal paid: \$ 15.63
Remaining balance: \$ 4673.48
Month 9
Minimum monthy payment: \$ 93.47
Principal paid: \$ 15.58
Remaining balance: \$ 4657.9
Month 10
Minimum monthy payment: \$ 93.16
Principal paid: \$ 15.53
Remaining balance: \$ 4642.37
Month 11
Minimum monthy payment: \$ 92.85
Principal paid: \$ 15.47
Remaining balance: \$ 4626.9
Month 12
Minimum monthy payment: \$ 92.54
Principal paid: \$ 15.42
Remaining balance: \$ 4611.48
Results
Total amount paid: 1127.35
Remaining balance 4611.48

What I should have:

Enter the outstanding balance on your credit card: 4800
Enter the annual credit card interest rate as a decimal: .2
Enter the minimum monthly payment rate as a decimal: .02
Month: 1
Minimum monthly payment: \$96.0
Principle paid: \$16.0
Remaining balance: \$4784.0
Month: 2
Minimum monthly payment: \$95.68
Principle paid: \$15.95
Remaining balance: \$4768.05
Month: 3
Minimum monthly payment: \$95.36
Principle paid: \$15.89
Remaining balance: \$4752.16
Month: 4
Minimum monthly payment: \$95.04
Principle paid: \$15.84
Remaining balance: \$4736.32
Month: 5
Minimum monthly payment: \$94.73
Principle paid: \$15.79
Remaining balance: \$4720.53
Month: 6
Minimum monthly payment: \$94.41
Principle paid: \$15.73
Remaining balance: \$4704.8
Month: 7
Minimum monthly payment: \$94.1
Principle paid: \$15.69
Remaining balance: \$4689.11
Month: 8
Minimum monthly payment: \$93.78
Principle paid: \$15.63
Remaining balance: \$4673.48
Month: 9
Minimum monthly payment: \$93.47
Principle paid: \$15.58
Remaining balance: \$4657.9
Month: 10
Minimum monthly payment: \$93.16
Principle paid: \$15.53
Remaining balance: \$4642.37
Month: 11
Minimum monthly payment: \$92.85
Principle paid: \$15.48
Remaining balance: \$4626.89
Month: 12
Minimum monthly payment: \$92.54
Principle paid: \$15.43
Remaining balance: \$4611.46
Results
Total amount paid: \$1131.12
Remaining balance: \$4611.46

Last edited by TheNightArrow : November 29th, 2012 at 09:42 AM. Reason: #indented line

#5
November 29th, 2012, 12:50 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 3,458
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403

I rearranged your program so that the computations appear once. You've written "minipay-principal" 3 times. Same with "balance*minipercent".

Suppose you're not allowed to pay fractional pennies, as I've done. That moves the answer a little bit closer to the expected result. Try other variations of that theme. Or you could use the decimal module.
Code:
```balance = float (raw_input ('1. What is the outstanding balance on the credit card?:'))
interest = float (raw_input ('2. What is the annual interest rate?:'))
minipercent = float (raw_input ('3. What is the minimum monthly payment rate?:'))
paid = 0
for month in range(1,13):
principal = interest/12.0*balance
minipay = round(balance*minipercent,2)
principalPaid = minipay-principal
balance -= principalPaid
paid += minipay
print 'Month', month
print 'Minimum monthy payment: \$',minipay
print 'Principal paid: \$',round((principalPaid),2)
print 'Remaining balance: \$',round(balance,2)
print 'Results'
print 'Total amount paid:', round((paid),2)
print 'Remaining balance', round((balance),2)```

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > My 1st time trying to write a program