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

Help with Program

OpenStudy (anonymous):

http://ocw.mit.edu/images/jquerybubblepopup-theme/grey/tail-bottom.png is the assignment -The problem on the assignment is Problem 2, Paying off Debt in a year. -This is what I currently have: ##calculates minimum fixed monthly payment needed in order to pay off credit card balance within 12 months outstanding_Balance_Credit_Card = raw_input("Outstanding Balance on Credit Card:") annual_Interest_Rate = raw_input("Annual Interest Rate:") month = 0 <= 0 monthly_Interest_Rate = annual_Interest_Rate / 12.0 updated_Balance_Each_Month = previous_Balance * (1 + monthly_Interest_Rate)

OpenStudy (turingtest):

your link doesn't work

OpenStudy (turingtest):

ok firstly, let's look at what you have so far: what is this line: ``` month = 0 <= 0 ``` ?

OpenStudy (anonymous):

well I was initiating a loop but, I incorrectin git

OpenStudy (anonymous):

Incorrectly wrote it*

OpenStudy (turingtest):

ok so, think about when we want our program to stop running. What should be the state of our remaining balance when the program ends?

OpenStudy (anonymous):

0 ..... thats why im questiong looping the month and not the balance

OpenStudy (anonymous):

less then or equal to 0 actually

OpenStudy (turingtest):

right, so we need to loop as long as the balance at the end of the year is greater than zero. In Pseudocode: ``` ## while balance not paid off ## for each month ## update the balance ## if balance is paid off, end loop ## print results ```

OpenStudy (anonymous):

ok so we are making a loop inside of a loop

OpenStudy (turingtest):

as you mentioned though, we don't have a minimum balance as input. The pset states you are supposed to start with 10 payments, and use multiples thereof

OpenStudy (turingtest):

that seems necessary to me, yes this is because we need to run through the months of the year as in the previous problem, but if when we get to the end we still have a positive balance due, we need to be able to loop back and try again with a different monthly payment. this indicates we need a double loop

OpenStudy (anonymous):

ok im going to attempt it and ill get back to you with my results!

OpenStudy (turingtest):

so what you need to do is initialize a minimum monthly payment, and then, as long as the balance is not paid off at the end of the year, we need to increase it by $10 great, I'll be here to help if i'm still online

OpenStudy (anonymous):

so the minimum monthly payment should start out as 10 and if it doesnt pay it off increment it by 10 until it does?

OpenStudy (anonymous):

and what do I make the balance equal to in the while loop, im guess that it is already equal to what we put in and I dont need to set it = to zero or anything

OpenStudy (turingtest):

exactly one slightly tricky part is that if you initialize the value to 10 before the while loop, then if you increase the amount by 10 inside the while loop you will start off checking 20, not 10 I recommend initializing it to 0 outside the loop, and adding 10 each time before the for loop

OpenStudy (turingtest):

the balance due is gotten from your raw_input statement. you need a different variable to represent the remaining balance inside the for loop, because if at the end of the 12 months the balance is not paid off, you need to be able to go back and access the original balance due

OpenStudy (turingtest):

you have a line that says ``` monthly_Interest_Rate = annual_Interest_Rate / 12.0 updated_Balance_Each_Month = previous_Balance * (1 + monthly_Interest_Rate) ``` but you never defined updated_balance_each_month or previous_balance you should make those two variables the same, and simply update the balance each time ``` currentBalance = currentBalance * (1 + monthlyInterestRate) ``` think about what currentBalance should be set to at the beginning of each year (every time we start the for loop)

OpenStudy (anonymous):

awesome I was wondering about previous balance, ill be back!

OpenStudy (turingtest):

good luck!

OpenStudy (anonymous):

thanks again sir

OpenStudy (turingtest):

oh save the formalities for your professor XD

OpenStudy (anonymous):

lol gotcha

OpenStudy (anonymous):

now im getting a syntax error with my while loop, I know Im not done im just trying to check out my work that ive done so far! ##calculates minimum fixed monthly payment needed in order to pay off credit card balance within 12 months outstanding_Balance_Credit_Card = raw_input("Outstanding Balance on Credit Card:") annual_Interest_Rate = raw_input("Annual Interest Rate:") ## while balance not paid off outstanding_Balance_Credit_Card = outstanding_Balance_Credit_Card while outstanding_Balance_Credit_Card (outstanding_Balance_Credit_Card > 1) month = 1 for month range(1,12) month + = 1 ## update the balance outstanding_Balance_Card_Card = current_Balance monthly_Interest_Rate = annual_Interest_Rate / 12.0 current_Balance = current_Balance * (1 + monthly_Interest_Rate) ## if balance is paid off, end loop ## print results print current_Balance

OpenStudy (turingtest):

issues i see so far: 1) ``` outstanding_Balance_Credit_Card = outstanding_Balance_Credit_Card ``` what is the point of this line? it does nothing but rebind a variable to itself; it's pointless. You want to bind the original balance to a new variable that we will update as we go through the loop, but this binding should occur *after* the while loop starts, but before the for loop (this way we reset the balance at the end of each year if we see the debt is not paid off 2) ``` while outstanding_Balance_Credit_Card (outstanding_Balance_Credit_Card > 1) ``` I don't understand the syntax of this line at all. Why do you have the varible twice, once compared to 1 ? aren't we concerned about when it reaches 0 ? Also, you forgot the colon at the end of the while statement. should be something like ``` while outstanding_Balance_Credit_Card > 0: ``` 3) ``` month = 1 for month range(1,12) month + = 1 ``` many things wrong here.everything you seem to be trying to accomplish here can be done with ``` for month range(1,13): ``` (again you forgot the colon) will bind the variable 'month' to 1, and on each iteration update it to the next month until (but not including) 'month' takes the value 12. You don't need to initiate the month variable to 1, and you don't need to do month += 1 each time; the 'for' statement does this on it's own. Also, this will only count months from 1 to 11, since range(a,b) counts from a to b-1, it does *not* include b. You should review how the range function works.

OpenStudy (turingtest):

as for the part of the pseudocode that says ## if balance is paid off, end loop this should be something like a check each month to see if the remaining balance is <=0, and if it is, we should then leave the loop. This probably means using a 'break' statement.

OpenStudy (anonymous):

sorry I didnt realize you answered it ill be back

OpenStudy (turingtest):

the overall design should be something like ``` outstanding_Balance_Credit_Card = raw_input("Outstanding Balance on Credit Card:") annual_Interest_Rate = raw_input("Annual Interest Rate:") ## initiate monthly payment (to what? you figure it out :P) ## while balance not paid off ## increase monthly payment by $10 ##store outstanding balance in a new variable like current_balance ## for each month of the year monthly_Interest_Rate = annual_Interest_Rate / 12.0 current_Balance = current_Balance * (1 + monthly_Interest_Rate) ## IF current balance is <0 ## store month we are in as a new variable like 'last_month' ## exit loop print last_month print monthly_payment print current_balance ```

OpenStudy (turingtest):

you can also do the line ``` monthly_Interest_Rate = annual_Interest_Rate / 12.0 ``` before any of the loops, actually, since that value won't change across the year and amounts for minMonthlyPayment

OpenStudy (anonymous):

yeah some of those dumb mistakes where me just checking and guessing

OpenStudy (turingtest):

Oh it's quite all right. Gotta write about 100 messy programs before they start looking nice. Just be careful about things like how the ``` for x in range(a,b): ``` work, because that kinda stuff is very important later on

OpenStudy (anonymous):

##calculates minimum fixed monthly payment needed in order to pay off credit card balance within 12 months outstanding_Balance = int(raw_input("Outstanding Balance on Credit Card:")) annual_Interest_Rate = float(raw_input("Annual Interest Rate:")) minimum_Monthly_Payment = int(raw_input("Minimum Monthly Payment:")) monthly_Interest_Rate = annual_Interest_Rate / 12.0 ## while balance not paid off while outstanding_Balance > 0: outstanding_Balance = current_Balance for month in range(1,12): ## update the balance outstanding_Balance = outstanding_Balance * (1 + monthly_Interest_Rate) ## if balance is paid off, end loop if outstanding_Balance <= 0: month = last_Month break print "RESULT" print "Monthly Payment to Pay Off Debit in 1 Year:",minimum_Monthly_Payment print "Number of Months Needed:",last_Month print "Balance:",outstanding_Balance this is what I got so far... I think the lack of sleep is really starting to get to me lol

OpenStudy (turingtest):

read what you wrote again after some rest and see if you notice any mistakes

OpenStudy (anonymous):

ok so I read it again and changed a few things around, basically I know I need to increment it by 10 each time so I added that but its not letting me assign outstanding balance to current balance. Obviously im doing something wrong so take a look at it if you can! ##calculates minimum fixed monthly payment needed in order to pay off credit card balance within 12 months outstanding_Balance = int(raw_input("Outstanding Balance on Credit Card:")) annual_Interest_Rate = float(raw_input("Annual Interest Rate:")) monthly_Interest_Rate = annual_Interest_Rate / 12.0 ## while balance not paid off while outstanding_Balance > 0: outstanding_Balance = outstanding_Balance + 10 outstanding_Balance = current_Balance for month in range(1,12): ## update the balance current_Balance = current_Balance * (1 + monthly_Interest_Rate) ## if balance is paid off, end loop if current_balance <= 0: month = last_Month break print "RESULT" print "Monthly Payment to Pay Off Debit in 1 Year:",minimum_Monthly_Payment print "Number of Months Needed:",last_Month print "Balance:",outstanding_Balance

OpenStudy (turingtest):

this line is backwards ``` outstanding_Balance = current_Balance ``` the value in the variable on the right is being put into the one on the left, hence you are trying to put something you have not yet defined: current_Balance, into the variable containing outsanding_Balance.

OpenStudy (turingtest):

assignments are read from right to left; you bind the value in the variable on the right to that on the left. your 'for' statement is still not quite right, because when you do ``` for x in range (a, b): ``` you iterate over the elements [a, a+1, a+2, ..., b-1], so what you have will cause months only to go up to 11, not 12

OpenStudy (turingtest):

``` ## update the balance current_Balance = current_Balance * (1 + monthly_Interest_Rate) ``` You are skipping a step in the monthly update: you also need to deduct the minimum payment, right? Your code will just make current_Balance get bigger and bigger....

OpenStudy (turingtest):

``` if current_balance <= 0: month = last_Month break print "RESULT" print "Monthly Payment to Pay Off Debit in 1 ``` indentation is wrong. after the 'break' statement you *exit the loop* which, in python, means you now write the code to be executed upon leaving the loop the the indentation outside the loop

OpenStudy (turingtest):

``` if current_balance <= 0: month = last_Month break print "RESULT" print "Monthly Payment to Pay Off Debit in 1 Year:",minimum_Monthly_Payment print "Number of Months Needed:",last_Month print "Balance:",outstanding_Balance ``` The indentation on the print statements is wrong. After the 'break' statement you *exit the loop* which, in python, means you now write the code to be executed upon leaving the loop the the indentation outside the loop

OpenStudy (turingtest):

by 'loop' i mean the loop before the 'if' statement, which, in this case, is the 'for' statement.

OpenStudy (anonymous):

omg thank you I was about to throw my laptop out the window because I couldnt assign the variable!

OpenStudy (anonymous):

ok so ive kind of gotten stumped, maybe im confused a bit with the break statement. So whenever I run the program it just breaks. ALSO so im trying to figure out how the program was running so I entered a print states ment and the while loop is running a bunch of times for month of the year so basically it run run the while loop 12 times before the current balance is changed so the month stays constant each time I print it out! ##calculates minimum fixed monthly payment needed in order to pay off credit card balance within 12 months outstanding_Balance = float(raw_input("Outstanding Balance on Credit Card:")) monthly_Payment = float(raw_input("Monthly Payment:")) annual_Interest_Rate = float(raw_input("Annual Interest Rate:")) monthly_Interest_Rate = annual_Interest_Rate / 12.0 current_Balance = outstanding_Balance ## while balance not paid off while outstanding_Balance > 0: current_Balance = current_Balance * (1 + monthly_Interest_Rate)- monthly_Payment for month in range(1,13): ## if balance is paid off, end loop if current_Balance <= 0:break print "RESULT:" print "Monthly payment to pay off debt in 1 year:",monthly_Payment print "Number of months needed:",month print "Balance:",current_Balance

OpenStudy (turingtest):

with what you have written, the balance will change twice. If you inserted a print statement to check the balance in the for loop this would make you think it only changed once, perhaps ``` monthly_Payment = float(raw_input("Monthly Payment:")) (1) current_Balance = outstanding_Balance ## (2) ## while balance not paid off while outstanding_Balance > 0: current_Balance = current_Balance * (1 + monthly_Interest_Rate) - monthly_Payment for month in range(1,13): if current_Balance <= 0:break ## (3) ## if balance is paid off, end loop ``` (1) The specs of the problem say to start with 10$ payments, not whatever you want, so you need to hard code a number to initialize monthly_Payment such that, on the first trial, you pay 10$/month . No raw_input statement should be here (2) you assign outstanding_Balance to current_Balance before we enter the while loop, hence this value will never be reset if it is not paid off at the end of the year (end of the for loop), which was the point of writing it. For this line to reset the balance to the original amount due it needs to be inside the loop, otherwise how can that assignment ever happen again? It should be executed every time we see that after 12 months, current_Balance is still > 0; (3) This is your update statement, but look where it is. You put it before the loop that iterates over the months of the year, so that means it is not included in what happens each month. i.e. You only update the balance once, and you do it at the beginning of the year.The only thing inside the for loop (the only thing you do *each month*) is the 'if' statement, which just checks to see if the balance has been paid off yet. So in effect, all your code does, is once a year (at the beginning of the while loop) update the balance. It never tries a new monthly amount to pay, and hence never pays any more than the last time you tried, and never goes below 0 (unless your initial amount pays off the debt on the first trial) You need to include a line that, before iterating over the months, but still inside a loop that continues until the balance is paid off, changes the amount paid each month by adding 10$, as per the specs of the problem. Also, i think break has to be indented on a new line: ``` if something: break ```

OpenStudy (anonymous):

##calculates minimum fixed monthly payment needed in order to pay off credit card balance within 12 months outstanding_Balance = float(raw_input("Outstanding Balance on Credit Card:")) annual_Interest_Rate = float(raw_input("Annual Interest Rate:")) monthly_Interest_Rate = annual_Interest_Rate / 12.0 ## while balance not paid off while outstanding_Balance > 0: current_Balance = outstanding_Balance current_Balance = current_Balance * (1 + monthly_Interest_Rate) - 200 for month in range(1,13): ## if balance is paid off, end loop if current_Balance <= 0: break print "RESULT:" print "Monthly payment to pay off debt in 1 year:",monthly_Payment print "Number of months needed:",month print "Balance:",current_Balance still getting nothing back! changed the monthly payment a few times

OpenStudy (anonymous):

##calculates minimum fixed monthly payment needed in order to pay off credit card balance within 12 months outstanding_Balance = float(raw_input("Outstanding Balance on Credit Card:")) annual_Interest_Rate = float(raw_input("Annual Interest Rate:")) monthly_Interest_Rate = annual_Interest_Rate / 12.0 monthly_Payment = 200 ## while balance not paid off while outstanding_Balance > 0: current_Balance = outstanding_Balance for month in range(1,13): current_Balance = current_Balance * (1 + monthly_Interest_Rate) - monthly_Payment ## if balance is paid off, end loop if current_Balance <= 0: break print "RESULT:" print "Monthly payment to pay off debt in 1 year:",monthly_Payment print "Number of months needed:",month print "Balance:",current_Balance this is actually what I have now....... so everything is running smoothly except where the break statement is. I took the break statement out and put it under the print statements and it just kepts running and then when I put it beffore the print statements like it is now it just doesnt print anything!! confussedd!!!!!!

OpenStudy (e.mccormick):

The problem I see is in the while loop. I do not know why you even have the for in there, but it seems you are never changing the outstanding_Balance, but you are using it over and over.

OpenStudy (anonymous):

``` ##calculates minimum fixed monthly payment needed in order to pay off credit card balance within 12 months outstanding_Balance = float(raw_input("Outstanding Balance on Credit Card:")) annual_Interest_Rate = float(raw_input("Annual Interest Rate:")) monthly_Interest_Rate = annual_Interest_Rate / 12.0 monthly_Payment = 200 ## while balance not paid off while outstanding_Balance > 0: current_Balance = outstanding_Balance for month in range(1,13): current_Balance = current_Balance * (1 + monthly_Interest_Rate) - monthly_Payment ## if balance is paid off, end loop if current_Balance <= 0: print "RESULT:" print "Monthly payment to pay off debt in 1 year:",monthly_Payment print "Number of months needed:",month print "Balance:",current_Balance ```

OpenStudy (e.mccormick):

It is this sort of error: ``` x = 10 y = 10 while x > 0 print x print y y = y - 1 ``` Because x never changes, the loop never ends. ``` while outstanding_Balance > 0: bla bla ``` You never change outstanding_Balance so the loop never ends.

OpenStudy (e.mccormick):

What your program does is tell how many months it will take to pay things off if a certain fixed payment is made, but only sort of. See, if the payment is not enough to pay it off, it dies. Try yours, as adjusted in chat, with a ballance of 3000. Should get some interesting results.

OpenStudy (e.mccormick):

Yes, that is what your program says i is trying to do, find the pay off in a year. But it does not really do that.

OpenStudy (anonymous):

for some reason I was under the impression that they wanted us to put it as 10 and increment it manually.........

OpenStudy (e.mccormick):

Well, they are saying to try different payments and see what happens. They wanted people to look at it three different ways to help show the power of the bisection search. There are other ways to make that sort of thing efficent enough for personal use, but they are trying to teach how to optimize for industrial use.

OpenStudy (e.mccormick):

Here is some logic for you. Case 1: Pay off 10 a month, if paying 10 a month does not make the outstanding balance do down to 0 in a year, pay off 20 a month. Keep increasing projected payments by 10 a month until you could pay it off in a year. Not too hard a calculation, but it has to loop through several failures before a success. Case 2: If you pay 1/12th of the outstanding balance, it seems like you will pay it off in a year. But there is interest. So paying off 1/12th falls short because of the interest. Still, it is closer than starting with a random payment value or just 10. Then you can increase up to what is needed. How much to increase? Well, look at what is left and /12. So you go up in smaller increments. The flow here is that you can have a situation where the increase is too small, so it never exists the loop.

OpenStudy (anonymous):

ok so what im going to do is initiate the first loop set the payment to 0 and then nest another loop that does the month and the payment increasing it by 10. sound right?

OpenStudy (e.mccormick):

For the 10 one, that is a good plan.

OpenStudy (e.mccormick):

As I said before, I do not always have the chat open. This is especially true if I am dealing with issues or answering questions. So it has been 40 min since you said something to me in chat, and I just saw it.

OpenStudy (e.mccormick):

In psudocode: ``` inputs from user for balance and APR testBalance = balance payment = 10 while testBalance > 0 for x = 1 to 12: testBallance = testBallance - payment testBallance = testBallance + interest formula goes here if testBallance > 0: payment = payment + 10 testBalance = balance print results ```

OpenStudy (e.mccormick):

If you look at that, it will stick in the while loop until the payment is high enough to pay off the debt in a year. It increases by 10 each time around. The for loop is to do the 12 monthly payments. You have two balances because you may need to reset. For large balances with a high APR, this is slow because it only jumps up a small bit each time and always starts at 10. As I said before, if you start with say `payment = balance / 12` then it will be a lot closer to what is needed and take less time.

OpenStudy (anonymous):

``` ##calculates minimum fixed monthly payment needed in order to pay off credit card balance within 12 months outstanding_Balance = float(raw_input("Outstanding Balance on Credit Card:")) annual_Interest_Rate = float(raw_input("Annual Interest Rate:")) monthly_Interest_Rate = annual_Interest_Rate / 12.0 monthly_Payment = 0 current_Balance = outstanding_Balance ## while balance not paid off while current_Balance > 0: months = 0 monthly_Payment += 10 while current_Balance > 0 and months < 12: months += 1 current_Balance = current_Balance * (1 + monthly_Interest_Rate) - monthly_Payment ## if balance is paid off, end loop if current_Balance <= 0: print "RESULT:" print "Monthly payment to pay off debt in 1 year:",monthly_Payment print "Number of months needed:",months print "Balance:",round(current_Balance, 2) ``` ahhh ok this is what I had I guess I need to move the monthly_Payment+=10 lower so that it adds the 10 after the loop

OpenStudy (e.mccormick):

Remember to reset each time through the outer loop.

OpenStudy (e.mccormick):

``` ## while balance not paid off while current_Balance > 0: current_Balance = outstanding_Balance months = 0 ``` See, that way it will do a fresh evaluation every loop.

OpenStudy (anonymous):

``` ##calculates minimum fixed monthly payment needed in order to pay off credit card balance within 12 months outstanding_Balance = float(raw_input("Outstanding Balance on Credit Card:")) annual_Interest_Rate = float(raw_input("Annual Interest Rate:")) monthly_Interest_Rate = annual_Interest_Rate / 12.0 monthly_Payment = 0 ## while balance not paid off while outstanding_Balance > 0: current_Balance = outstanding_Balance months = 0 monthly_Payment += 10 while months < 12: months += 1 current_Balance = current_Balance * (1 + monthly_Interest_Rate) - monthly_Payment ## if balance is paid off, end loop if current_Balance <= 0: print "RESULT:" print "Monthly payment to pay off debt in 1 year:",monthly_Payment print "Number of months needed:",months print "Balance:",round(current_Balance, 2) ``` so the program is running smoothly I just cant get the loop to stop when I need it to. I inserted a break statment but, it either doesnt stop or wont print out what I need lol. EXTREMLEY FRUSTRATING....... I even changed the nested while loop to while months < 12 and current_Balance >0 so I dont understand why the loop kepts going when the current balance reaches 0!!!

OpenStudy (e.mccormick):

You are still making the mistake where you evaluate a temp variable but not doing the loop based on it.

OpenStudy (e.mccormick):

Here, let me toss it out this way: ``` ##calculates minimum fixed monthly payment needed in order to pay off credit card balance within 12 months outstanding_Balance = float(raw_input("Outstanding Balance on Credit Card:")) current_Balance = outstanding_Balance annual_Interest_Rate = float(raw_input("Annual Interest Rate:")) monthly_Interest_Rate = annual_Interest_Rate / 12.0 monthly_Payment = 0 ## while balance not paid off while current_Balance > 0: current_Balance = outstanding_Balance months = 0 monthly_Payment += 10 while months < 12: months += 1 current_Balance = current_Balance * (1 + monthly_Interest_Rate) - monthly_Payment ## if balance is paid off, end loop if current_Balance <= 0: print "RESULT:" print "Monthly payment to pay off debt in 1 year:",monthly_Payment print "Number of months needed:",months print "Balance:",round(current_Balance, 2) ``` I have not run it, but try it that way.

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!