Hey guys, I am currently working on ps1 question 2 under the spring2011 section. I am confused as to why my code doesn't produce the correct answer. Can you please help me? This is the what I have: balance=float(raw_input("What is the outstanding balance on your credit card?")) annint=float(raw_input("What is the annual interest rate as a decimal")) payment=10 newbalance=balance while balance>=0: for month in range(1,13): newbalance=balance*(1+(annint/12))-payment if newbalance<=0: break else balance=newbalance payment=payment+10 Thanks!
I put some print commands between your code and I see that the balance does not decrease, also, be sure to use the correct indentation for the code. Let me review it again to find out where you may be having the problem
I see the error in your code, in the for loop, you need newblance to have the new value after each month, you cannot use balance in the newbalance=balance*(1+annint/12) - payment, because you will always use the original balance amount, that's one problem. it should be newbalance=newbalance*(1+annint/12) - payment. Then in the for you need to have an if statement to break the for loop, if newbalance is <=0. I hope this will help :), but use print statements in your code to find out where you may have an error (debug).
This is the arranged code while balance > 0: for month in range(1,13): newbalance = newbalance*(1+(annint/12))-payment #print newbalance #print payment if newbalance <=0: break #check what happened with the newbalance using payment of 10 #if not less then increase payment and calculate it again if newbalance <= 0: balance = newbalance else: newbalance = balance payment = payment+10
Thanks for your response! But I don't completely understand why my balance wouldn't change. Doesn't the last line of else and then the change of balance to newbalance change the value for the loop?
balance is a constant . Lets say for instance the balance entered by user is $5000. This means that balance has a value of $5000. so if you look ar your fo loop below: for month in range(1,13): newbalance=balance*(1+(annint/12))-payment python reads it as for month in range(1,13): newbalance=5000*(1+(annint/12))-payment over and over and over again within the for loop. But remember that the newbalance should change every month for 12 months within the for loop. I think thats the point that xavigus is making.
ok, thanks for your responses!
Hi Markz, you enter a balance, and you need to find out the best minimum payment so you can totally pay the balance in one year. Since you enter the balance, you need to keep the original amount somewhere, and you need to use a clone of it to calculate when it is reduced to 0. Newbalance will be your clone, so you need to use that to calculate the interest and substract the minimum payment, so starting with a payment of $10, you add interest then substract payment, and you have a newbalance after the first month, in the for loop, you need to do that for 12 months, but at the same time you need to check if in any of the 12 months your balance is 0 or negative value, which means you paid off. If the for loop after 12 months continues with a positive newbalance, that means, your minimum payment needs to change, so the next logic will be under the while loop. If newbalance is 0 or negative, you assign that value to balance, so the while loop ends, if newbalance continues to be positive, then increase the minimum payment and re-assign to newbalance the original amount you owe so you can test it again, in the for loop, using the new min payment.
This is a descriptive logic of the solution clonebalance=originalbalance # initial minimum payment minimumpayment=10 while originalbalance > 0 for month 1 to 12 newbalance=newbalance*(1+(annint/12)) - minimumpayment if new balance <=0 #I paid off on or before 12 months exit the for loop end if statement next month in for loop # after the for loop, need to check if I need # to continue in the while loop if newbalance <= 0 #last value from the for loop originalbalance = newbalance # so the while condition can end else #we need to try a new min payment increase min payment by 10 give newbalance the original balance end if statement Hope it helps :)
clonebalance is newbalance, I meant that.
Did not see Shirley's answer, yes, that's the point.
I understand now. Thanks for the description at each step. I think the way I wrote it increased the payment each month rather than running through all 12 months and then increasing if newbalance>0. On to the next lecture now!
Join our real-time social learning platform and learn together with your friends!