I am stuck on Problem Set 2 Question 1. I posted the question on here: http://stackoverflow.com/questions/25795479/paying-the-minimum/25798294?noredirect=1#comment40352508_25798294 Hopefully someone can assist me.
You need to do a loop within a loop to test for when you have it right.
Sorry I am not following. Why would i need to do a loop within a loop? Why is my calculation different from theirs?
Ah, I was thinking of the other problem where they have you figure out the minimum payment to pay it off in a year. Hmm. Let me look again.
So you are doing the minium monthy payment as a percentage?
Also, rather than make to month variables, you could print month +1.
two, that is.
Yes min_month = .02. Would that have an effect on my calculations?
No, just figuring things out. It is some sort of minor math issue. Hmm... It is not off by much, but it adds up.
outstand_balance = round((outstand_balance - Min_monthly_payment), 2) Where is the interest being added in?
If we compare month 12 mine and theirs: Mine: Month: 12 Minmimum monthly payment $76.87 Principle Paid $12.81 Remaining Balance $3751.27 Theirs: Month: 12 Minimum monthly payment: $92.54 Principle paid: $15.43 Remaining balance: $4611.46 Why is my principle decreasing?
You are not charging the interest.
What is done standardly: new ballance = old ballance + interest - payment What is in your code: new ballance = old ballance - payment
I thought I was doing that with the Remaining_balance variable. Should I remove that variable?
I am not sure you even need it.
so it should be oustand_balance = outstand balance + interest/12 - monthly payments?
You ave code to find Interest_paid. Might as well use the variable.
Hmm It seemed like it worked but now I am missing the remaining balance variable to keep track
Use the outstanding. Unless you make the remaining outside the loop, you need to use the outstanding. And sinceyou do not need outstanding at the end, it is worthless to duplicate it just for the loop.
awesome thanks for the assistance
Want to see a tweaked version of it?
please
``` ## Rounded here, so no need to round later on ballance because ## everything done to it is rounded. outstand_balance = round(float(raw_input('Enter the outstanding balance on your credit card:')),2) ## Did the monthly calc once rather than each month mon_interest_rt = float(raw_input('Enter the annual credit card interest rate as a decimal:'))/12 min_month_rt = float(raw_input('Enter the minimum monthly payment as a decimal:')) for month in range (0,12): Min_monthly_payment = round((min_month_rt * outstand_balance), 2) Interest_accrued = round((mon_interest_rt * outstand_balance), 2) Principle_paid = round((Min_monthly_payment - Interest_accrued), 2) outstand_balance = outstand_balance - Min_monthly_payment + Interest_accrued ## See, just month, not month and Month. print "Month: " + str(month + 1) print "Minmimum monthly payment $" + str(Min_monthly_payment) print "Principle Paid $" + str(Principle_paid) print "Remaining Balance $" + str(outstand_balance) ```
So, that all make sense? It is not really a lot of changes, but they are some things to think about like do the work once rather than 12 times.
you changed the ann int rate and just obtained the month rt right? Or is there more that I dont see?
and the rounded
Also, I rounded the input for the ammount rather than rounding it every time it is recalculated.
Because these are rounded: ``` Min_monthly_payment = round((min_month_rt * outstand_balance), 2) Interest_accrued = round((mon_interest_rt * outstand_balance), 2) ``` They will only add or subtract rounded numbers. So by rounding outstand_balance at the start, I don't need to also round it inside the loop because it will never change away from being rounded.
I see what you mean now. I am trying to find the total amount paid which I assume is (min monthlypayment + principle payment). Do I put this inside the loop or outside the loop? for the Results section.
For a small program, no big difference. For a big program doing tens or hundreds of thousands of calculations, doing less can add up. For total paid, put it outside the loop then add to it inside the loop. After the loop, print it, unless you need a running total (paid so far.)
And if you are only doing the min payment, it would only be adding up the min payment.
Sorry I dont get what you mean by put it outside the loop then add it inside the loop. Also why isnt principle paid not included in the total amount paid?
Because you are only paying the minimum payment. It does not matter if it is applied to interest or principal.
I see. Can you explain the loop part again, please.
Have they gone over scope yet?
The scope of a variable is where it can be used. If a variable is made inside a function or loop, it can only be used inside that function or loop. So if you need to use a variable outside a loop, it MUST be declared outside the loop. There is a bit more to scope, but that is the basics of it.
not that I recall
This is psudocode (fake code): ``` x = 1 for loop print x x = x + 1 ``` That would print out x as it counted up. But this: ``` for loop x = x + 1 print x ``` would cause an error because x does not exist outside the loop.
In your code: ``` Min_monthly_payment = round((min_month_rt * outstand_balance), 2) Interest_paid = round((mon_interest_rt * outstand_balance), 2) Principle_paid = round((Min_monthly_payment - Interest_paid), 2) ``` Those only exist inside the loop. They can not be used outside the loop. So if you want to do some total of the payments, you need to make it (declare it) before the loop, then add to it inside the loop, and then you can use it wherever.
I see what you mean now.
I am going to head out, but here is some fun I had with your code: ``` original_ballance = round(float(raw_input('Enter the outstanding balance on your credit card:')),2) outstand_balance = original_ballance mon_interest_rt = float(raw_input('Enter the annual credit card interest rate as a decimal:'))/12 min_month_rt = float(raw_input('Enter the minimum monthly payment as a decimal:')) total_payment = float(0) for month in range (0,12): Min_monthly_payment = round((min_month_rt * outstand_balance), 2) total_payment += Min_monthly_payment Interest_accrued = round((mon_interest_rt * outstand_balance), 2) Principle_paid = round((Min_monthly_payment - Interest_accrued), 2) outstand_balance = outstand_balance - Min_monthly_payment + Interest_accrued print "Month: " + str(month + 1) print "Minmimum monthly payment $" + str(Min_monthly_payment) print "Principle Paid $" + str(Principle_paid) print "Remaining Balance $" + str(outstand_balance) print "\nTotal ammount paid $" + str (total_payment) print "So your debt went from $" + str( original_ballance ) + " to $" + str( outstand_balance ) +" while you paid $" + str (total_payment) principal_paid = original_ballance - outstand_balance print "That means $" + str(principal_paid) + " went to the principal and $" + str(total_payment - principal_paid) + " went to interest." ``` Run that on the 4800, .2, .02.
thanks for the explanations.
np. Have fun!
Join our real-time social learning platform and learn together with your friends!