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

I'm having difficulty with Problem Set 1. Not so much difficulty but wondering how to shorten the code and use a loop. The problem is about paying off your credit card debt and in this example i use 4800 - balance, annual interest rate .2, and minimum monthly payment .02. Thinking about it for a couple hours I thought maybe I could use a while loop but am not sure how to. So simply put the code I have is long and repetitive and I want to shorten it. Will post code in a comment since it doesnt seem to allow me to now

OpenStudy (anonymous):

bal = float(raw_input("Enter the outstanding balance on your credit card: ")) anrate = float(raw_input ("Enter the annual credit card interest rate as a decimal: ")) minrate = float(raw_input ("Enter the minimum monthly payment rate as a decimal: ")) intpay = anrate / 12 * bal monpay = bal * minrate prinpay = monpay - intpay print "Month 1" print "Minimum Monthly Payment: ", round(monpay, 2) print "Principle paid: ", round(prinpay, 2) print "Remaining Balance: ", round(bal - prinpay, 2) rembal = bal - prinpay monpay = minrate * rembal print "Month 2" print "Minimum Monthly Payment: ", round(monpay, 2) intpay = anrate / 12 * rembal prinpay = monpay - intpay print "Principle paid: ", round(prinpay, 2) print "Remaining Balance: ", round(rembal - prinpay, 2) rembal = rembal - prinpay monpay = minrate * rembal print "Month 3" print "Minimum Monthly Payment: ", round(monpay, 2) intpay = anrate / 12 * rembal prinpay = monpay - intpay print "Principle paid: ", round(prinpay, 2) print "Remaining Balance: ", round(rembal - prinpay, 2) rembal = rembal - prinpay monpay = minrate * rembal print "Month 4" print "Minimum Monthly Payment: ", round(monpay, 2) intpay = anrate / 12 * rembal prinpay = monpay - intpay print "Principle paid: ", round(prinpay, 2) print "Remaining Balance: ", round(rembal - prinpay, 2)

OpenStudy (e.mccormick):

Normally you would use a loop to work through the repetition. Here is an overview of the concept: http://www.tutorialspoint.com/python/python_loops.htm Click where it says "For loop" to see a couple examples with code.

OpenStudy (anonymous):

I understand the concept of it i'm just having extreme difficulty figuring out what to do. I figured out i need a for i in range of (1,13) because im doing 12 months. But i have no idea how to make it loop and use the remaining balance rather than the initial balance and its become irritating.

OpenStudy (e.mccormick):

Well, if you look at it, the only thing that is changing in the above is the month you are printing out. Everything else is the same. Well, look at this loop: ``` for month in range (1,5): print month ``` That prints out: 1 2 3 4 How it works is this: 1) Anything indented is in the loop. 2) The range sets it to start at 1 and end BEFORE 5, so 1, 2, 3, and 4 but not 5. Normally it would start at 0, but you have months 1 to 4 in your bit of code, so 2 started at 1. 3) It sets month to the first number in the range, goes through the indented lines, then goes back and sets month to 2... 4) Repeat until done. So if you were to add other parts of your code and change the print to `print "Month " + month` it would do the same thing, but use less code.

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!