我想在python中製作一個計算信用卡上剩餘餘額的程序。它用於MIT開放課件"Introduction to Computer Science and Programming"。我在做problem set one。如何更改python for循環中的變量?
程序必須要求用戶啓動變量:起始餘額,年利率和最低月度支付。 這是我的代碼。
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))
如何獲得剩餘餘額以正確更新?我無法弄清楚如何在每個月底更新餘額。到目前爲止,每個月的最低月還款額,本金支付和剩餘餘額都會返回相同的值。