A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • one year ago

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!

  • This Question is Closed
  1. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  2. Curry
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  3. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  4. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  5. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.