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

problem set 2

OpenStudy (anonymous):

Yessss... is there an actual question?

OpenStudy (anonymous):

I wanted to ask about the second problem in the set. I don't seem to find the right solution to this. Anyone help?

OpenStudy (anonymous):

There are 3 parts for the second problem set. There are clear instructions on how to get to the required solution. Is there a specific questions that you have? As mentioned in another post, before the deadline for the submission of this problem set, we cannot give the answers. However, we can do as much as we can to help you understand the problem so you can solve it on your own!

OpenStudy (anonymous):

pagesa = 10 BalanceRe = balance while balance > 0: for month in range(1,13): BalanceRe = (BalanceRe - payment)*(1 + annualInterestRate/12) if BalanceRe <=0: break if BalanceRe <= 0: balance = BalanceRe else: BalanceRe = balance pagesa = pagesa + 10 print('Lowest Payment: ' + str(pagesa)) I cannot find a problem with this code but still it does not produce the right answer. Where may I be wrong?

OpenStudy (anonymous):

The first obvious problem I can see is that you have two identical if conditions: if BalanceRe <=0: break if BalanceRe <= 0: balance = BalanceRe If BalanceRe is indeed negative, then going into the first if statement, it will break from the loop. The next if statement will never be executed. The next issue is that you have the if condition within the for loop which if the BalanceRe is not negative you keep increasing the pagesa variable by 10. So every month you will keep increasing the payment. Is that correct?

OpenStudy (anonymous):

Yes that is correct. I increase the pagesa variable by 10 for the next loop. Just like the problem suggests

OpenStudy (anonymous):

I believe by "next loop" it means the next iteration to find the balance. i.e. after you have tried for 12 months and you haven't found the correct payment, then you add 10 and retry for another 12 months

OpenStudy (anonymous):

Yes that is correct. Sorry for my mistake. When i execute this code my answers are 10 times bigger than the real ones. At a specified balance, for exampe, it has to be 40 and my answer is 400. I can't seem to find this...

OpenStudy (anonymous):

Well, fix the two errors we discussed above and post the new code... You need to first understand how to solve it in paper and then use programming to make your life easier! It seems that you do not have a proper solution for the problem at hand.

OpenStudy (turingtest):

I would also suggest incrementing pagesa and resetting balanceRe=balance before the for loop. That way you don't need the conditionals at all (besides the while loop).

OpenStudy (anonymous):

OK. I tried the code like this: payment = 0 while balance > 0: payment = payment + 10 newBalance = balance for month in range(1,13): newBalance = (newBalance - payment)*(1 + annualInterestRate/12) print('Lowest Payment: ' + str(payment)) Does not work. I don't know what else to do...pls help :)

OpenStudy (turingtest):

your while loop will never terminate because "balance" is what is given by the computer and so is always greater than 0. It never changes. So what value should you put >0 for the while loop?

OpenStudy (turingtest):

think about it, you are very close

OpenStudy (turingtest):

@ErionT there are two things you need to do to fix this program. Let me know when you are back and I will help you think through them.

OpenStudy (anonymous):

The loop never ends, you have to update balance value, everybody tries that balance be positive, that's why is endless when balance is greater than zero, change that while loop, if you want it for a year use better a for, and the while loop inside of that.

OpenStudy (turingtest):

I mentioned that the loop never ends because balance is not updated. Let me pm you my solution @Gianko15 I don't see why you would want the while loop after the for.

OpenStudy (anonymous):

Sorry I should've seen other answer, We agree on that. I mention that the for loop will be before, because I thought it was the same calculation everymonth to get the balance, due to the annual interest rate.

OpenStudy (anonymous):

Also because the nweBalance should be calculated even if is negative.

OpenStudy (turingtest):

The program is to calculate minimum monthly payment needed to pay off a debt with interest withing a year. The method they ask for here is exhaustive enumeration by multiples of ten. There is not need to iterate after newBalance becomes negative, as when you reach that point you have found the minimum payment withing your margin of error.

OpenStudy (turingtest):

The program is to calculate minimum monthly payment needed to pay off a debt with interest withing a year. The method they ask for here is exhaustive enumeration by multiples of ten. There is no need to iterate after newBalance becomes negative, as when you reach that point you have found the minimum payment withing your margin of error.

OpenStudy (anonymous):

Hello back. I guess i have to place the variable newBalance in the while loop? The problem is I have only 3 tries left before all 30 expire :) This is my last code. Does not work too...:( payment = 0 for month in range(1,13): newBalance = (newBalance - payment)*(1 + annualInterestRate/12) while newBalance > 0: payment = payment + 10 balance = newBalance print('Lowest Payment: ' + str(payment))

OpenStudy (anonymous):

You don't have a value for newBalance, it'll be negative always, so you won't enter to the while loop, shouldn't newBalance be equal to = (balance - payment)*(1+AIR/12). And there is when you need balance (the actual balance), payment and the rate.

OpenStudy (anonymous):

I have also changed the code to what you say and it still does not work.

OpenStudy (anonymous):

I really don't know when the calculations will satisfy what you wanna to get, see this code and tell me if it's like you want to get, I'll put it exactly as I just wrote it on Python. newBalance = int(raw_input('balance: ')) payment = int(raw_input('payment: ')) annualInterestRate = int(raw_input('rate: ')) for month in range(1,13): ## print month newBalance = (newBalance - payment)*(1 + annualInterestRate/12.0) #float for rates if newBalance > 0: #if is greater than zero: I don't know the exactly rules #print newBalance payment = payment + 10 #increase payment newbalance = newBalance-payment #decrease newBalance to balance the if loop #print payment,newBalance else: #if is negative make it postive and add the payment newBalance = (newBalance*-1)+payment print('Lowest Payment: ' + str(payment))

OpenStudy (turingtest):

@gianko we really discourage giving out complete answers on this site, that why I showed you mine by pm

OpenStudy (turingtest):

I would say you were closer to a cleaner solution with what you had earlier: payment = 0 while balance > 0: payment = payment + 10 newBalance = balance for month in range(1,13): newBalance = (newBalance - payment)*(1 + annualInterestRate/12) print('Lowest Payment: ' + str(payment)) the problem here is that balance is always greater than zero; the program does not change that value instead, use the value that *is* changing and that *can* become zero you will find that if you use that you will have to initiate a different value for that variable before the while loop so that it is not undefined. also your print statement should not be indented at all

OpenStudy (anonymous):

So sorry @TuringTest, so silly I didn't realize, won't happen again.

OpenStudy (turingtest):

No worries @Gianko15 , thanks for understanding :)

OpenStudy (anonymous):

Great, I thought it was something he wanted, instead of something about edX, I guess when I'll forward i'll find it.

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!