• 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.

Looking for something else?

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