Search code examples
python-3.xwhile-loop

Python While loop Problems


I need a little help with this while loop. I have my Initial balance and a current balance. Is what I am trying to do is after the first initial balance the current balance needs to become the Initial and so on.

InitalPrice = float(input("Enter the Price of the Computer: "))
Month = 0
AnInterest = (InitalPrice - InitalPrice * .10) * .01 / 12
MonthlyPayment = (InitalPrice - InitalPrice * .10) * 0.05 
Principal = MonthlyPayment - AnInterest

print("%0s%20s%20s%20s%13s%23s" %("Month", "Current Balance", "Interest Owed", "Principal Owed", "Payment", "Balance Remaining"))


while MonthlyPayment >= 0:    
    Month += 1
    InitalPrice = InitalPrice - InitalPrice * .10 
    Balance = InitalPrice + AnInterest - MonthlyPayment

    print("%0d Months%20.2f%20.3f%20.2f%13.2f%23.2f" %(Month, InitalPrice, AnInterest, Principal, MonthlyPayment, Balance))


    if Balance <= 0:
        break

Solution

  • Based on your description, I think this is more along the lines of what you are trying to do. I could be wrong about how the economics of a loan like this works, but I think you subtract the principal from the loan each month, recalculate your interest owed, and continue until you've payed off the entire balance. Let me know if this is wrong.

    # Total price
    price = float(input("Total computer cost:"))
    
    # Price after down payment
    remaining_price = (price * 0.9)
    
    # Monthly payment (5 percent of remaining price)
    monthly_payment = remaining_price * 0.05
    
    # Interest owed for this month
    interest_owed = remaining_price * (.12/12.0)
    
    # Principal for this month
    principal = monthly_payment - interest_owed
    
    # Month 
    month = 0
    
    # Print stuff
    print("%0d Months%20.2f%20.3f%20.2f%13.2f%23.2f" %(month, price, interest_owed, principal, monthly_payment, remaining_price))
    
    while remaining_price > 0:
        month += 1
        remaining_price -= principal
        interest_owed = remaining_price * (.12/12.0)
        principal = monthly_payment - interest_owed
    
        print("%0d Months%20.2f%20.3f%20.2f%13.2f%23.2f" %(month, price, interest_owed, principal, monthly_payment, remaining_price))
    

    There may be the edge case of the last month where you have officially paid off the balance, but I'm not sure how interest is calculated in that case.