【发布时间】:2018-05-02 16:17:01
【问题描述】:
每次我在 edX.org 上的 Python 课程中,我都得到了 11 或 12 的正确答案,但在讨论中没有得到任何人的帮助,因为没有人可以真正在那里发布任何代码(没有真正的帮助)并且课程中似乎没有任何可用的支持人员可以与之交谈,我会为此付费,所以我在这里发帖。我正要花钱请人来辅导我,但现在没有人可用,而且我承受着一定的压力,要在 12 月之前完成这门课程以完成我的工作。
这是作业:
现在编写一个程序,计算在 12 个月内还清信用卡余额所需的最低每月固定还款额。固定的每月付款是指一个数字,每个月都不会改变,而是每个月都会支付的固定金额。
在这个问题中,我们不会处理最低月付款率。
以下变量包含如下所述的值:
balance - 信用卡上的未结余额
annualInterestRate - 年利率小数
程序应打印出一行:在 1 年内偿还所有债务的最低每月付款,例如:
最低付款:180
假设按月末余额(当月还款后)按月复利。每月付款必须是 10 美元的倍数,并且所有月份都相同。请注意,使用此付款方案,余额可能会变为负数,这没关系。所需数学的摘要如下:月利率=(年利率)/12.0
每月未付余额 =(以前的余额)-(每月最低固定还款额)
每月更新余额=(每月未付余额)+(每月利率x每月未付余额)
这是我的代码:
#! /usr/bin/python3.6
from math import ceil
def roundup(x):
return int(ceil(x / 10.0) * 10)
def getFixedPayment(balance,annualInterestRate,counter=12):
totalLoan = balance + (balance*annualInterestRate)
monthlyPayment = roundup(totalLoan/12.0)
newBalance = totalLoan - monthlyPayment
if counter < 12:
newPayment = newBalance / counter + 1
else:
newPayment = newBalance / counter
if counter == 1:
return roundup(newPayment/12)
else:
return getFixedPayment(balance,annualInterestRate,counter-1)
#balance = 3329
#annualInterestRate = 0.2
print('Lowest Payment: ' + str(getFixedPayment(balance,annualInterestRate)))
这是测试结果:(我这里有 15 个,所以你可能能够识别出我看不到的模式。标记为“错误”的是我得到的错误)
Test Case 1
balance = 3329; annualInterestRate = 0.2
Output:
Lowest Payment: 310
Test Case 2
balance = 4773; annualInterestRate = 0.2
Output:
Lowest Payment: 440
Test Case 3
balance = 3926; annualInterestRate = 0.2
Output:
Lowest Payment: 360
Randomized Test Case 1
balance = 265; annualInterestRate = 0.18
Output:
Lowest Payment: 30
Randomized Test Case 2
balance = 263; annualInterestRate = 0.18
Output:
Lowest Payment: 30
Randomized Test Case 3
balance = 317; annualInterestRate = 0.25
Output:
Lowest Payment: 30
Randomized Test Case 4
balance = 720; annualInterestRate = 0.2
Output:
Lowest Payment: 70
Randomized Test Case 5
balance = 4284; annualInterestRate = 0.2
Output:
Lowest Payment: 400
Randomized Test Case 6
balance = 3834; annualInterestRate = 0.15
Your output:
Lowest Payment: 340
*** ERROR: Expected Lowest Payment: 350
, but got Lowest Payment: 340
***
Correct output:
Lowest Payment: 350
Randomized Test Case 7
balance = 3045; annualInterestRate = 0.18
Output:
Lowest Payment: 280
Randomized Test Case 8
balance = 4461; annualInterestRate = 0.2
Output:
Lowest Payment: 410
Randomized Test Case 9
balance = 4657; annualInterestRate = 0.04
Your output:
Lowest Payment: 370
*** ERROR: Expected Lowest Payment: 400
, but got Lowest Payment: 370
***
Correct output:
Lowest Payment: 400
Randomized Test Case 10
balance = 3395; annualInterestRate = 0.2
Your output:
Lowest Payment: 320
*** ERROR: Expected Lowest Payment: 310
, but got Lowest Payment: 320
***
Correct output:
Lowest Payment: 310
Randomized Test Case 11
balance = 4045; annualInterestRate = 0.15
Your output:
Lowest Payment: 360
*** ERROR: Expected Lowest Payment: 370
, but got Lowest Payment: 360
***
Correct output:
Lowest Payment: 370
Randomized Test Case 12
balance = 3963; annualInterestRate = 0.18
Output:
Lowest Payment: 360
【问题讨论】:
-
当你手工处理错误案例时,你得到了正确的答案吗?
-
就像评分机一样,时好时坏。
-
你对你知道答案应该是什么感到满意吗?
-
嗨卡洛斯,不,不是真的。那是我的结果的打印输出。除了 1st 3 的结果,这些都是课程评分者的随机结果。
-
我发现这个问题是如何与规范、自己的尝试和一整套测试用例一起出现的,但仍然有人想为“为什么这段代码不起作用?”而关闭它。原因。