anonymous
  • 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!
MIT 6.00 Intro Computer Science (OCW)
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
chestercat
  • chestercat
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
Curry
  • 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.
Curry
  • 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.
anonymous
  • 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.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • 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
anonymous
  • anonymous
actually, rereading your code, you do need it, indeed

Looking for something else?

Not the answer you are looking for? Search for more explanations.