1

以下是我试图用来获取结果的代码,但出现错误:

import numpy

array = [-1000,0,0,1094.60,0,0,1094.60]

for b in array:
    a = round(numpy.irr(array), b-1) * 100 
print (round(a,2))

错误:

TypeError: 'float' object cannot be interpreted as an integer

但是,只需替换那个“b-1”就可以让我的代码工作,但我不能使用它,因为数组可以尽可能大或尽可能小。我不允许手动输入替换“b-1”的数字。以下是相同的工作代码。

import numpy

array = [-1000,0,0,1094.60,0,0,1094.60]

for b in array:
    a = round(numpy.irr(array), 6) * 100 
print (round(a,2))

我需要一种方法来自动处理任何大小的数组。

4

1 回答 1

1

您收到错误是因为您尝试将速率四舍五入到小数点后 1093.60 位(这是第 4 次迭代的 b-1)。

numpy.arr适用于任何大小的数组。你不必给它一个大小(你没有)。我了解到您正在尝试以百分比形式打印利率,四舍五入到小数点后 2 位。改用这个:

rate = numpy.irr(array) * 100
print "rate =", round(rate, 2)

在我之前给你的代码的末尾使用它会给出这个输出以供比较:

Present Value (P) at rate 0.0125 = 0.217803365612
Present Value (P) at rate 0.01875 = -0.143198101517
Present Value (P) at rate 0.015625 = 0.0349275148787
Present Value (P) at rate 0.0171875 = -0.0547196000329
Present Value (P) at rate 0.01640625 = -0.0100432897584
Present Value (P) at rate 0.016015625 = 0.0124051532756
Present Value (P) at rate 0.0162109375 = 0.00117171042648
rate = 1.62
于 2016-02-26T23:49:58.023 回答