Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 15 Online
OpenStudy (anonymous):

Prob set 1(a).Total balance keeps coming up short. I just do not get what I am missing. Any hint or help would be much appreciated

OpenStudy (anonymous):

##CREDIT CARD PAYMENT CALC balance = float(raw_input ('Enter your full balance:')) rate = float(raw_input ('Enter annual interest rate as decimal:')) minrate = float(raw_input ('Enter the minimal rate as decimal')) for x in range(1,13): print "Month",(x) print "Minimum monthly payment:$",(round(minrate*balance,2)) print "Principle paid:$",(round(minrate*balance - rate/12*balance,2)) total = (round(minrate*balance*(x),2)) balance =-(balance*minrate-rate/12*balance-balance) print "Remaining balance:$",(round(balance,2)) print ("RESULT") print ("Total amount paid"),total print ('Remaining balance at end of year'),(round(balance,2))

OpenStudy (anonymous):

Hi Markpj, It looks like what you're doing in your code is calculating a total paid based on the balance at the start of the month, the monthly payment rate, and what month you're in. This actually produces an approximation of the total- not the true total. I would recommend that with each iteration in your for loop you just add what was paid during the current month to a running total. I added some lines to your code. It prints the total for each month now. You can see how the calculated total diverges from the summed total as the months go on. Hope this helps! Dan ##CREDIT CARD PAYMENT CALC balance = float(raw_input ('Enter your full balance:')) rate = float(raw_input ('Enter annual interest rate as decimal:')) minrate = float(raw_input ('Enter the minimal rate as decimal')) flSummedTotal = 0.0 for x in range(1,13): print "Month",(x) print "Minimum monthly payment:$",round((minrate*balance),2) print "Principle paid:$",round((minrate*balance - rate/12*balance),2) print'***DEBUGGING***' #ADDED LINE total = (minrate*balance*(x)) flSummedTotal += round((minrate*balance),2) #ADDED LINE print 'Calculated total = ', total #ADDED LINE print 'Summed Total = ', flSummedTotal #ADDED LINE print '***END DEBUGGING***' #ADDED LINE balance =-(balance*minrate-rate/12*balance-balance) print "Remaining balance:$",round((balance),2) print ("RESULT") print ("Total amount paid"),round(total,2) print ('Remaining balance at end of year'),(round(balance,2))

OpenStudy (anonymous):

will look over carefully. Thank you.

OpenStudy (anonymous):

I fixed it but do not understand why the + symbol in front of total carries the balance. total +=(round(minrate*balance,2)) Thank you.

OpenStudy (anonymous):

The syntax x += y is a shorthand for x = x + y. The right-hand-side is evaluated before the assignment so the value of x is updated.

OpenStudy (anonymous):

Yes, thank you, jonghyun for clearing that up for me. Now I can resume.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!