我正在尝试用python制作一个计算信用卡余额的程序。这是麻省理工学院开放课件“计算机科学与编程导论”。我正在做问题集一。
该程序必须要求用户提供起始变量:起始余额、年利率和最低每月付款。这是我的代码。
initialOutstandingBalance= float(raw_input('What is the outstanding balance on your
card?'))
annualInterestRate=float(raw_input('What is the annual interest rate expressed as a
decimal?'))
minimumMonthlyPaymentRate=float(raw_input('What is the minimum monthly payment rate on
your card expressed as a decimal?'))
for month in range(1,13):
print("Month: "+ str(month))
minimumMonthlyPayment=float(minimumMonthlyPaymentRate*initialOutstandingBalance)
interestPaid=float((annualInterestRate)/(12*initialOutstandingBalance))
principalPaid=float(minimumMonthlyPayment-interestPaid)
newBalance=float(initialOutstandingBalance-principalPaid)
print("Minimum monthly payment: $"+str(minimumMonthlyPayment))
print("Principle paid: $"+str(principalPaid))
print("Remaining Balance: $"+str(newBalance))
如何让余额正确更新?我不知道如何在每个月底更新余额。到目前为止,每个月都会返回相同的最低每月付款、已付本金和剩余余额的值。