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

PS1, B: This is a question about the solution set. Why is line 4, newbalance=balance and the distinctions between "newbalance" and "balance" needed in the while, for, if, else sections? In other words, why does using "balance" only throughout calculate an incorrect answer? Here is an excerpt of my code using only "balance" resulting in an incorrect calculation: while balance > 0: for month in range (1,13): balance= balance*(1+r/12)-minpay if balance <=0: break if balance >0: minpay= minpay +10 else: print 'RESULT' Thanks!

OpenStudy (anonymous):

There are two variations of the MIT 6.00 course. It would be helpful if you included the following: 1. The course itself 2. Your entire solution. It will allow others to run it.

OpenStudy (anonymous):

I'm doing the Spring 2011 version. Sorry, I was having trouble pasting in before so shortened my code to the excerpt only. balance=float(raw_input('Enter the outstanding balance on your credit card: ')) r=float(raw_input('Enter the annual credit card interest rate as a decimal: ')) minpay=10 while balance > 0: for month in range (1,13): balance= balance*(1+r/12)-minpay if balance <=0: break if balance >0: minpay= minpay+10 else: print 'RESULT' print 'Fixed monthly payment: ', minpay print 'Number of months needed: ', month print 'Ending balance: ', balance The correct answer creates a placeholder newbalance in line 4: balance = float(raw_input('Enter the outstanding balance on your credit card: ')) interest = float(raw_input('Enter the annual credit card interest rate as a decimal: ')) minPay = 10 newBalance = balance #Calculate balance left by paying $minPay each month for 12 (or fewer) months. #Add $10 to minPay and repeat until balance goes to 0 or less. while balance > 0: for month in range(1,13): newBalance = newBalance*(1+(interest/12))-minPay if newBalance <=0: break if newBalance <= 0: balance = newBalance else: newBalance = balance minPay = minPay+10

OpenStudy (anonymous):

You can attach the code with your question.

OpenStudy (anonymous):

Attached is your solution. I use python 3.2 (should not make a difference). I included both versions. My observations are: 1. in Solution1, minPay is incremented annually (Solution 2 does it Monthly) 2. Solution1 boosts balance every 12 months. I don't think this is correct (I have not read the question). Hope this allows you to arrive at the correct answer.

OpenStudy (anonymous):

He (the man who wrote the solution) uses two variables because he uses one of them (newbalance) to sort out the problem, and the other one as a reference (balance). *If applying the formula of the problem, newbalance is negative at the end, it's done. And then he assigns the value of the reminder to the variable balance, and he prints it out. *If the value of newbalance is positive, the problem is not sorted out, then he assigns the initial value of balance to newbalance, increments the value of minpay, and starts the loop again. Sorry for my level of english, lol. I hope you can understand my answer.

OpenStudy (anonymous):

@albertitobr as the *woman* who wrote the solution for this class, I'd appreciate it if you didn't jump to assuming all computer scientists are male. Plenty of us are female! And yes, the reason we use two variables here is because we are referring to two different quantities. The "newbalance" is the new balance that occurs after each month. The "balance" refers to the original balance (perhaps it is more clear to call this variable "origbalance" or something). Recall we're doing a bisection search here to try to find the best monthly payment value. We expect to have to do a few guesses before we find the correct (most optimal) value for monthly payment. If our guess is wrong (see we have two clauses, perhaps the guess is too high or too low) we will need to adjust our guess and try again. But if we never hung on to the original inputted balance, we could never have the capability to try to re-guess! I hope that vaguely helps. I strongly suggest rewatching the lecture and recitation videos on bisection search.

OpenStudy (anonymous):

OH it's true. Sorry I appologize for it. I didn't even notice I was talking about a "man". Please forgive me.

OpenStudy (anonymous):

No worries :) it's just something I notice a lot, being a woman in a male-dominated field. A gender-neutral way to refer would just be "person" - doesn't make an assumption if a woman or man!

OpenStudy (anonymous):

I'm not a native english speaker, in my language I would have used a neutral word but in english is hard for me to think so many things at the same time. ;) In fact I began to watch these open courses to improve my english listening.

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!