【发布时间】:2012-08-19 22:16:54
【问题描述】:
我是 python/编程初学者。我被分配了一个关于 MIT 开放课件的问题:
编写一个程序来计算每月最低固定还款额,以便在 12 个月内还清信用卡余额。
将以下浮点数作为raw_input():
1) 信用卡上的未结余额 2) 年利率为小数
打印出固定的最低还款额、偿还债务所需的月数(最多 12 个月,可能少于 12 个月)以及余额(可能为负数)。
假设利息按月初的余额按月复利(当月还款前)。每月付款必须是 10 美元的倍数,并且所有月份都相同。请注意,使用此付款方案,余额可能会变为负数。
答案是:
balance = float(raw_input('Enter the outstanding balance on your credit card: '))
interest = float(raw_input('Enter the annual credit card interest rate as a decimal: '))
minPay = 10
newBalance = balance
while balance > 0:
for month in range(1,13):
newBalance = newBalance*(1+(interest/12))-minPay
if newBalance <=0:
break
if newBalance <= 0:
balance = newBalance
else:
newBalance = balance
minPay = minPay+10
print 'RESULT'
print 'Monthly payment to pay off debt in 1 year: ' + str(minPay)
print 'Number of months needed: ' + str(month)
print 'Balance: ' + str(round(balance,2))
我的问题:
1) 使用 1200 作为原始输入余额,使用 0.18 作为利率。有人能用文字解释一下你将如何得出 minPay = 120、month = 11 和 balance = - 10.05 吗?
我对 newBalance = newBalance* (1 +(interest/12)) - minPay 感到困惑。
使用 1200 作为余额将使 newBalance = 1200 * (1 +(.18/12)) - 10 = 1218 - 10 = 1208。
2) 由于 newBalance 不是
我无法理解这个问题。任何见解都将不胜感激。
【问题讨论】:
-
请正确格式化您的代码。您还可以通过点击代码按钮来格式化内联代码。我会为你做一个sn-p。
-
拿起笔和纸,开始像 python 那样解释代码。
-
换个角度看。如果最低还款额不足以在规定时间内支付余额怎么办?
-
@zerkms,也称为试运行en.wikipedia.org/wiki/Dry_run_(testing)。考虑到您在湿件上运行程序,这个名字很奇怪。
-
@gnibbler:现在我知道它的正确“营销”名称了,谢谢 :-)
标签: python while-loop