Hi I have a question regarding Pset 1 part A I made a for loop that calculates credit card payment for 12 months. I wrote "for x in range (0, 11, 1):" but my output only gives answers up to Month 11. My answers are all identical to the ones given in the pset, but I don't have Month 12. I can solve the issue by writing (0, 12, 1), and I would get an output that is exactly identical to the pset. However, I thought (0, 11, 1) means repeat 12 times?
Also, for pset 1 part B, I feel that I hit a similar problem. If I put (0,11,1), I would get the correct test case 1 scenario, but the wrong test case 2, and if I put (0,12,1), I get the wrong test case 1 and the right test case 2. I am not sure what I am missing.
range(a, b) in Python gives you a list of numbers taking steps the size of the optional third argument. Just like in indexing, the stopping element is *omitted* in the list that results from calling range. range(a, b) = [a, a+1, a+2,... b-2, b-1] i.e. range(2, 7) = [2, 3, 4, 5, 6]
at least that is the case for Python 2.7.x
It would be easier to debug if you were to post your code. The iteration limitations, as discussed by @TuringTest , are the same in Python 3.3.x but do not explain why you are not getting the results expected in part B.
use it as in range(1,12,1) ?? Doesn't that help?
Thanks for the response guys. I have attached my code for your reference! Thanks so much for the help!
So if I change pset1 part A's range for the for loop to (0,12,1), then there are no problems. With pset 1 part B, I still have the same issue: If I put (0,11,1), I would get the correct test case 1 scenario, but the wrong test case 2, and if I put (0,12,1), I get the wrong test case 1 and the right test case 2.
It is because you are only working off 12 months. If you put a condition inside your loop you will see that it will stop early. for x in range (0, 12, 1): if(balance > 0): balance = balance* (1 + annualint/12) - payment
thnx for the response. Sorry I'm not sure I understand. Shouldn't we be working off 12 months? Isn't the if(balance >0) the same as the "while (balance > 0.0)?" Aren't we supposed to make it stop when the balance goes below zero? Thanks!
The question wants you to pay it off in no more than 12 months, but what if you pay it off earlier, your code executes 12 iterations regardless of the balance value. In this instance you are using the while loop to control the increasing payment. All of the balance reduction is happening within your for loop. The reason your answers do not match the test case is because you are running one more iteration than they are. By adding a conditional within the for, you ensure that the math is not calculated on a balance less than 0. You will have to add some tuning to print out the proper 'x', but I don't see you having an issue with that. >>> Balance: 1200.0 Annual Interest: 0.18 Monthly payment: 120 Number of months needed: 11 End Balance: -10.05 >>> Balance: 32000.0 Annual Interest: 0.2 Monthly payment: 2970 Number of months needed: 12 End Balance: -74.98 >>>
cool, so you mean that the for loop will finish all its iterations before checking back with the condition of the while loop? The while loop doesn't break the for loop once balance becomes negative?
also for the for loop, is there is difference between (0, 12) and (0, 12, 1)? They both increment by 1 per iteration, right?
The while loop evaluates after completion of the for. When your payment is only $10, your 12 months is only 120 and so your balance is greater than zero, you increment payment by 10 and then hit the for loop again. You have to wait until your payment is great enough and your for loop brings the balance below zero before the while will drop out. As for your second question, there is no difference between the two. The 3rd argument is an optional step size. You could have just as easily done (0,.12, .01) and had the same result.
http://en.wikibooks.org/wiki/Python_Programming/Loops you can think of nested loops like the odometer of a car; the inner loop is the rightmost wheel and the outer loop is the leftmost wheel. http://dpaste.com/1291950/
Join our real-time social learning platform and learn together with your friends!