>balance = float(raw_input("Enter the outstanding balance on your credit card")"/> >balance = float(raw_input("Enter the outstanding balance on your credit card")"/> >balance = float(raw_input("Enter the outstanding balance on your credit card")"/> >balance = float(raw_input("Enter the outstanding balance on your credit card")"/>
Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 22 Online
OpenStudy (anonymous):

I'm on problem set 1, question 2. I tried nesting a "for" within a "while": >>balance = float(raw_input("Enter the outstanding balance on your credit card")) >>annualinterestrate = float(raw_input("Enter the annual credit card interest rate")) >>monthlyinterestrate = annualinterestrate/12.0 >> >>monthlypayment = 0 >>while balance > 0: >> monthlypayment += 10 >> for month in range (1,13): >> balance = balance + round(balance*monthlyinterestrate,2) - monthlypayment This didn't work -- the monthly payment amount was usually too small and the resulting balance was well below $0. After looking at the solution, I incorporated two elements. First, I included an "if" statement to break my "for" look once balance <= 0. Second, I made balance = initialbalance BEFORE my "while" loop AND a second time WITHIN the while loop, just before the "for" loop. See: >>initialbalance = float(raw_input("Enter the outstanding balance on your credit card")) >>annualinterestrate = float(raw_input("Enter the annual credit card interest rate")) >>monthlyinterestrate = annualinterestrate/12.0 >> >>monthlypayment = 0 >>balance = initialbalance >>while balance > 0: >> balance = initialbalance >> monthlypayment += 10 >> for month in range (1,13): >> balance = balance + round(balance*monthlyinterestrate,2) - monthlypayment >> if balance <= 0: >> break This worked. I think I understand why I needed the "break" -- the "for" loop goes to 12, even if the 0 balance is reached at month 11. But why the need to create an initialbalance AND balance variable? Why didn't my single "balance" variable do the trick? And why the need to assign initialbalance to balance TWICE? I got it to work, but I'm not positive why the difference. Thanks!

OpenStudy (curry):

Um, it actually doesn't seem like you need two variables. Perhaps you're altering the variable somewhere else? Also, I don't what the out put is and what it should be. But you're not using initial balance anywhere again, so it doesn't seem like you need it.

OpenStudy (curry):

The one other thing i can think of is maybe there is a discrepancy between the type of variable initialbalance, balance, and the actual input are.

OpenStudy (anonymous):

Thanks! This was my output: print "RESULT" print "Monthly payment to pay off debt in 1 year: $" + str(monthlypayment) print "Number of months needed: " + str(month) print "Balance: $" + str(balance) The first balance=initialbalance is necessary -- I get a syntax error without it. I think I can't enter the loop without defining balance. Without the second balance=initialbalance, I get the wrong answer--I think it's necessary to reset balance, otherwise I start the second "for" loop iteration with the balance resulting from the first iteration. That's my best guess.

OpenStudy (anonymous):

indeed, @ereitz; if you don't reset balance you end up using the result from the previous iteration. but you don't need the outer loop condition anymore

OpenStudy (anonymous):

actually, rereading your code, you do need it, indeed

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!