-2

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
6
  • I don't understand exactly what you're intending to compute (and there are a lot of likely errors), but a big obvious problem with your code is that you're checking MonthlyPayment >= 0 as the condition for the while loop, but never changing MonthlyPayment at any point. That means the condition will always be true (if it was true at the start) and so the loop will never end. Probably you want to be checking if Balance or something related is positive instead. Commented Jun 3, 2016 at 3:07
  • I have changed the MonthlyPayment >= 0 to InitialPrice , I know there are some errors, but I'm an basically making a program take the price of an item and figures the monthly payment, principal, intrest. The initial Price is the price after the 10% down Payment. I need the program run until all payments are made and the remaining balance is zero. I'm just trying to fix one thing at a time. Commented Jun 3, 2016 at 3:11
  • Can you talk us through exactly what you are computing? I think you need to be resetting some values, like MonthlyPayment on each loop iteration, but it's unclear. Commented Jun 3, 2016 at 3:12
  • This is the program I have to make.'''The credit plan at TidBit Computer Store specifies a 10% down payment and an annual interest rate of 12%. Monthly payments are 5% of the listed purchase price, minus the down payment. Write a program that takes the purchase price as input. The program should display a table, with appropriate headers, of a payment schedule for the lifetime of the loan. Each row of the table should contain the following items: Commented Jun 3, 2016 at 3:18
  • the month number (beginning with 1) the current total balance owed the interest owed for that month the amount of principal owed for that month the payment for that month the balance remaining after payment The amount of interest for a month is equal to balance * rate / 12. The amount of principal for a month is equal to the monthly payment minus the interest owed.''' Commented Jun 3, 2016 at 3:19

2 Answers 2

0

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.

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

1 Comment

Thank you for your help.
0

i was able to get it with this

print("Enter the purchase price:")
purchasePrice = input()
purchasePrice = float(purchasePrice)
downPayment = purchasePrice * 0.10
monthlyPayment = (purchasePrice - downPayment) * 0.05
annualInterestPayment = purchasePrice * 0.12
month = 0
balance = purchasePrice - downPayment

print("the purchase price is", purchasePrice)
print("the down payment is", downPayment)
print("the monthly payment is", monthlyPayment)
print("the annual interest is", annualInterestPayment)

#print("012345678901234567890")
print("%0s%20s%20s%20s%11s%22s" %("Month","Starting Balance","Interest to Pay","Principal to Pay","Payment","Ending Balance"))
#first %#s marks how far in it will go.
#with the h in month being placed where the number in %#s indicates
while balance > 0.0:
    month += 1
    if monthlyPayment > balance:
        currentBalance = balance
        monthlyPayment = balance
        currentInterest = 0
        currentPrincipal = monthlyPayment-currentInterest
        endingBalance = currentBalance - currentPrincipal
        print("%0d%20.2f%20.2f%20.2f%13.2f%23.2f" %(month, currentBalance, currentInterest, currentPrincipal, monthlyPayment, endingBalance))

    else:    
        currentBalance = balance
        currentInterest = (currentBalance * 0.12)/12
        currentPrincipal = monthlyPayment-currentInterest
        endingBalance = currentBalance - currentPrincipal
        print("%0d%20.2f%20.2f%20.2f%13.2f%23.2f" %(month, currentBalance, currentInterest, currentPrincipal, monthlyPayment, endingBalance))
    balance = endingBalance

blckknight, your advise helped, so i set the loop to check for the balance being greater than 0. This stooped the infinite loop my nested loop kept making. One small thing i want to align, are the numbers. How do i set the alignment so the 10 doesnt shift the line back after the 9 in months

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.