0

What I'm trying to do in the while loop is iterate the payments by an integer of 10 so that if that integer (g) fails to get the CBalance <= 0 within a 12 months period then all of the variables reset except for g, which goes up by 1.

Balance = float(raw_input('Enter Balance '))
Rate = float(raw_input('Enter interest rate '))
monthlyI = Rate/12
month = 0
g = 1
Payment = 10
CBalance = Balance
while CBalance > 0:
    Payment = Payment*g
    month += 1
    CBalance = CBalance *(1+ monthlyI)- Payment
    if month > 12:
        month = 0
        g += 1
        Cbalance = Balance
3
  • Your code seems to be doing what you said. What is the question ? Commented Jan 5, 2013 at 19:59
  • 4
    Just a comment: Please do not capitalize variable names in Python, that's almost always reserved for classes. PEP 8 has more information. Commented Jan 5, 2013 at 20:15
  • @frb: Actually PEP 8 doesn't say CapWords names are to only be used for classes. Commented Jan 6, 2013 at 3:12

2 Answers 2

1

I think I finally understand what your question is about and what's causing the problem -- namely a simple misspelling of a variable name. To fix it, just change the last line of the statements following the if in your while loop from:

        if month > 12:
            month = 0
            g += 1
            Cbalance = Balance

to:

        if month > 12:
            month = 0
            g += 1
            CBalance = Balance  # note corrected spelling of variable name on left

Which explains why all the values weren't being reset. It would have been helpful if you explicitly mentioned which variable it was in your question if you knew it. Anyway, this sort of thing is more likely to happen when one uses Capitalized and mixedCase variable names as you are doing.

Many programmers try to avoid them for that reason, is especially with languages like Python where you generally don't have to declare variables before using them. You might want to check out the Naming Conventions section of PEP 8's style guidelines.

Sign up to request clarification or add additional context in comments.

2 Comments

Let me clarify. The code works well if the balance can be less than or equal to zero in a 12 month period. My problem is that if balance cannot go down in a 12 month period I need the loop to start again with all of the variables having the original value except for g. If I input a balance of 120 and an interest rate of .1 then it will give me a balance of -3.03 with month = 0 and g = 2.
Actually what you added didn't help much, except that it to get me thinking about your question again...and suddenly I had an epiphany and have updated my answer accordingly. Hope I got it right.
0

What I'm trying to do in the while loop is iterate the payments by an integer of 10 so that if that integer (g) fails to get the CBalance <= 0 within a 12 months period then all of the variables reset except for g, which goes up by 1.

I think what's happening is that each time you run this while you will get:

Payment = 10 * 1 //First while payment = 10

2nd time

Payment = 10 * 1 //payment = 10 again.

Which results in:

  CBalance = CBalance * (1 + monthlyI) - 10

Which can never won't go to a negative value, which is needed to end the loop?

While you probably want:

counter = 1;
while CBalance > 0:
   Payment = Payment*counter
   month += 1
   counter += 1
   CBalance = CBalance *(1+ monthlyI)- Payment
   if month > 12:
      month = 0
      counter = 1
      g += 1
      Cbalance = Balance

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.