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

This question is about out of loop variables. I know this is a possible answer for Problem Set 1 - 3: http://pastebin.com/zvRShXEM My first version had the montly_payment variable definition (or whatever you call that) outside the loop: http://pastebin.com/r90hJS8y Can you help me figure out why do I need that line inside the while loop and before the for?

OpenStudy (anonymous):

if I'm correct, you need to do define the variables before the actual loop starts; however, you need to do monthly payment as : monthly_payment =( (minimum_payment + maximum_payment) / 2.0), so as to create the definition of the variable which will include the division of two. If you do that, I believe that it won't matter where you put the definition of monthly payment, as long as it's before the loop starts.

OpenStudy (anonymous):

If you put monthly_payment before the while then script doesn't work. It only works if you put if below the while and above the for. I don't understand why.

OpenStudy (anonymous):

did you make the change to the code?

OpenStudy (anonymous):

Hi miniludovica, I don't have problems on making this work (the second link is the right solution). Mine is a "what if and why" question. I moved the monthly_payment to the right position (second link) and it worked. But I don't know what's the "science" behind my first wrong answer (first link). Thanks

OpenStudy (anonymous):

hm, that's really weird. i tried your code, and I see the difference. It probably has to do something with the initiation conditions, I'm sorry I couldn't help; however, I'll ask my professor tomorrow about it.

OpenStudy (anonymous):

You have to have the monthly_payment inside the while loop because if it is outside, the monthly payment will remain unchanged while your program's run. Hence, since you're testing the same value over and over, you get the same balance each iteration. This will make your program run in an infinite loop since it can't find the right value (Well, unless your starting value makes the balance 0) . However, when it is inside the while loop, it's value's updated each iteration enabling you to search for the optimal value of the monthly payment. :)

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!