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

Hello everyone! I'm doing problem set 1, problem 3. I was reading the solution code, but don't quite understand the existence from line 21 to line 25. I don't think this for loop affects the result but it turns out that it does. Could anyone just enlighten me a little bit on this? Thank you so much!

OpenStudy (anonymous):

Sorry I'm new to here. Guess I should post the code as well. And also, I'm learning the latest 6.00sc. The solution code is as below: # 6.00 PS1-C Solution # Uses bisection search to find the fixed minimum monthly payment needed # to finish paying off credit card debt within a year # Retrieve input original_balance = float(raw_input("Enter the outstanding balance on your credit card: ")) interest_rate = float(raw_input("Enter the annual credit card interest rate as a decimal: ")) # Initialize state variables balance = original_balance low_payment = balance/12 high_payment = (balance*(1+(interest_rate/12))**12)/12 # Use bisection search until the search space is sufficiently small while True: balance = original_balance monthly_payment = (low_payment + high_payment)/2 # Simulate passage of time until outstanding balance is paid off # Each iteration represents 1 month 21 for month in range(1,13): 22 interest = round(balance*interest_rate/12, 2) 23 balance += interest - monthly_payment 24 if balance <= 0: 25 break if (high_payment - low_payment < 0.005): # Bisection search space is small enough # Print result print "RESULT" # Round monthly payment up to the nearest cent monthly_payment = round(monthly_payment + 0.004999, 2) print "Monthly payment to pay off debt in 1 year:", round(monthly_payment,2) # Recompute remaining balance and the number of months needed balance = original_balance for month in range(1,13): interest = round(balance*interest_rate/12, 2) balance += interest - monthly_payment if balance <= 0: break print "Number of months needed:", month print "Balance:", round(balance,2) break elif balance < 0: #Paying too much high_payment = monthly_payment else: #Paying too little low_payment = monthly_payment I've listed the 21-25 where I'm confused with. Thank you so much!

OpenStudy (anonymous):

It is simulating how much interest will be added to the entire yearly paid total right? so this would obviously have an effect on the results because without the interest the total balance would be lower!

OpenStudy (anonymous):

but of course, the interest added would be lower every month because of payments so... it is in a for loop to also remove a certain amount from the balance based on the monthly payment!

OpenStudy (anonymous):

Gotcha! Thank you so much!

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!