Output of the code 这是我正在处理的财务问题。我设置了一个循环,但是在显示所有正确值之后,它无限显示每个变量的所有“ 0”。我的Python版本是在Ubuntu上运行的3.6.7。
我试图进行设置,以使current_balance大于ending_balance,但是问题仍然存在。
price = float(input("Enter initial price: "))
INTEREST_RATE = 0.12 / 12
DOWN_PAYMENT = price * .9
monthly_payment = 0
ending_balance = 0
interest = 0
principal = 0
month = 0
print("%s%18s%10s%11s%9s%13s" % ("Month", "Current Balance", "Interest", "Principal", "Payment", "End Balance"))
month = 1
current_balance = DOWN_PAYMENT
interest = current_balance * INTEREST_RATE
monthly_payment = current_balance * 0.05
principal = monthly_payment - interest
ending_balance = current_balance - principal
while ending_balance > 0:
print("%d%18.2f%10.2f%11.2f%9.2f%13.2f" % (month, current_balance, interest, principal, monthly_payment, ending_balance))
month += 1
current_balance = ending_balance
interest = current_balance * INTEREST_RATE
monthly_payment = current_balance * 0.05
principal = monthly_payment - interest
ending_balance = current_balance - principal
没有错误,只是无限循环。一旦end_balance = 0,程序应结束。
答案 0 :(得分:1)
浮点数是您的问题。要结束循环,请使用:
while ending_balance >= 0.005:
当前余额总是越来越小,但只显示两位小数。