while循环迭代相同的答案

时间:2016-04-03 19:36:10

标签: python

第一次在这里发布海报和初学者。如果一个人每月只支付信用卡公司要求的最低月度付款,我试图在一年后运行一段时间来确定信用卡余额。问题是循环迭代相同的答案:

bal = float(raw_input("Enter your outstanding balance: "))
ann = float(raw_input("Enter your annual interest rate: "))
min = float(raw_input("Enter the minimum monthly payment rate "))

month = 1
#min_monthly_paym=nt = min * bal
#interest paid = ann/12.0 * bal
#principal paid = min_monthly_payment(MMP) -interest paid(IP)
#remaining balance = bal - principal paid (PP)

mmp = min * bal
ip = ann/12.0 * bal
pp = mmp - ip
rb = bal - pp

while month <= 12:  
    print "Month:", month
    print "Minimum monthly payment:", mmp
    print "Principle paid:", pp
    print "Remaining balance:", rb
    month = month + 1

因此它会经历所有月份,但只会打印出这些计算的初始值。我尝试在打印语句之后将mmp / ip / pp / rb设置为彼此,但同样的事情发生了。我想我需要创建另一个变量但不确定是什么。

1 个答案:

答案 0 :(得分:4)

您正在执行以下操作:

a = 1
ten_times_a = a * 10

for n in range(10):
    print ten_times_a     # Output: 10, 10, 10, 10, ...

在上面的示例中,ten_times_a在执行时计算

它没有定义将ten_times_a 更新为a更改的公式或规则。

您必须将重新计算放在for循环的正文中。

for a in range(10):
    ten_times_a = a * 10
    print ten_times_a     # Output: 0, 10, 20, 30, 40, ...