Python while循环问题

时间:2016-06-03 02:53:50

标签: python-3.x while-loop

我需要一点帮助这个循环。我有我的初始余额和当前余额。我想要做的是在第一次初始平衡后,当前余额需要成为初始等等。

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

1 个答案:

答案 0 :(得分:0)

根据您的描述,我认为这更符合您要做的事情。关于像这样的贷款的经济​​学如何运作我可能是错的,但我认为你每个月从贷款中扣除本金,重新计算你欠的利息,并继续直到你已经偿还了全部余额。如果这是错误的,请告诉我。

# 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))

上个月可能存在正式支付余额的边缘情况,但我不确定在这种情况下如何计算利息。