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

I have another question. This time with the second problem in the first pset. I'm sorry to ask again, but I am the only one I know who is taking any sort of cs.

OpenStudy (anonymous):

http://dpaste.com/hold/785090/ I know I am doing something very silly but i can't put my finger on it.

OpenStudy (anonymous):

At this point I am here: http://dpaste.com/hold/785111/

OpenStudy (anonymous):

you are supposed to have a fixed monthly payment in one year according to your code the monthly payment is increasing by 10 each and every month.

OpenStudy (anonymous):

I used the solution that they gave us to make it work, but I still don't understand it. http://dpaste.com/hold/785318/ Can someone explain lines 16, 18, and 21. Thanks!

OpenStudy (anonymous):

Line 16: if ub < 0: This checks to see if ub (variable that contains the updated balance) is less than zero. If it is less than zero, line 17 (break) causes the program to break out of the for loop begun at line 14. line 18: Once again, checks to see if ub < 0. In, this case if ub (the updated balance) is less than zero, it (line 19) sets the outstanding balance (ob) equal to the updated balance (ub) line 21: This part is what happens if ub was NOT found to be less than zero on line 18. It sets the updated balance to the original balance then increments the monthly payment by 10 (line 22)

OpenStudy (anonymous):

Awesome! Thank you so very much!. I was not understanding line 21, which was my original problem. I'm going to see if I can work on a different way to write the code.

OpenStudy (anonymous):

If line 17 causes it to break to line 14 why is it not in an infinite loop?

OpenStudy (anonymous):

Since it is less than 0

OpenStudy (anonymous):

It's not causing it to break TO line 14, it's causing it to break the for loop that started at line 14.

OpenStudy (anonymous):

Think of the code like this: While there would still be money owed, (1) run the for loop .. add in the amount that'd be due that month, break out if it'd cause overpayment (2) if they still haven't paid enough add $10 to the monthly payment (that'll be used the next time through the while loop), If they have paid enough in this iteration of the while loop, set our final values so they'll print 'cause this is the last run through of the while loop The whole thing is about incrementing the monthly payment and heading back through the while loop until it hits a monthly payment that's big enough to get it paid within a year. One thing that's been helping me is having a debugger. I know it's not listed as necessary for this course but I'm using wing101 (totally free) and it lets you step through the code line by line by hitting f7 and has a window where you can watch the variables change as you execute each line of code. I really recommend it.

OpenStudy (anonymous):

Taking it to it's simplest form ... assume for the moment that there's no interest (mainly because it's more convenient for me to explain sans interest): Say, the total cost is $80. The program would start, monthly cost starts at $10, the while loop starts, the for loop starts and runs through 8 months (8*10) before the whole cost is paid, the for loop ends, the price per month stays at $10/month, and it's done. If the cost is $90, the same thing happens, but it ends at 9 months ... all the way up to $120 being 12 months at $10/month. Once you try for a price of $130 though, you go into the while loop and the entire for loop runs through all 12 months one time without fully paying ... so it ups the monthly price by $10. Now you're at $20/month, the next time through the while loop it checks to see how many months it would take at $20 per month which is $130/20 or 7 months. Once you try for a price of $250, it ends up needing to go up to $30 per month before it can get paid within 12 months, etc, etc. Once you get that, you just add in the interest component which increases the monthly payment by annual interest/12. I hope that made sense, feel free to ask for clarification or PM me if it didn't.

OpenStudy (anonymous):

Dude, you are a life saver. I greatly appreciate your help. I also downloaded the WING IDE and it is awesome. I think it is going to help bunches. Again, my sincerest gratitude for all the effort you went through to break it down for me. Serious kudos.

OpenStudy (anonymous):

Anytime, I'm glad you like it ... I really just can't imagine coding without a debugger. *grins* I'm totally spoiled these days, and, yes, wing101 seriously rocks. I'm just starting this class, don't know any python yet but I've got a pretty extensive background in a few other languages. I'm always willing to lend a hand if I can.

OpenStudy (anonymous):

In that case we might get to be friends quickly. :P

OpenStudy (anonymous):

try the same problem set now using nested for loop. don't use the while loop

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!