比较两个百分比值,直到它们相等

时间:2016-03-08 18:17:13

标签: python excel numpy xlrd

import xlrd,numpy
excel = '/Users/Bob/Desktop/'

wb1 = xlrd.open_workbook(excel + 'assignment3.xlsx')
sh1 = wb1.sheet_by_index(0)

colA,colB = [],[]
for a in range(3,sh1.nrows):
    colA.append(int(sh1.cell(a,0).value))
    colB.append(int(sh1.cell(a,1).value))
print(colA)
print(colB)

given_per = (sh1.cell_value(2,1))*100
print("Given Percentage:", given_per)

calc_per = numpy.irr(colB) * 100  # Using this formula to get the 5%
print("Calculated Percentage", round(calc_per, 0), '%', '\n')

#Given percentage (10%) is the required percentage.We are getting 5%.
#Change values 1275 and 400 because they have the word "change" beside 
#them in a case when given_per > calc_per such that the adjustment
#of those values make the calc_per >= given_per. 
# NOTE: Don't make a change directly in the excel file.

if calc_per >= given_per:
    print("Percentages are equal.")  # In our case they aren't.
else:
    print("Percentages are NOT equal.")
    # Adjust 1275 and 400. Print the adjusted colB as new_colB
    # Print the adjusted values in B6 and B8
    # And display the re-calculated calc_per as new_calc_per
    # NOTE: Make sure that this code is generic. It should work for any excel
    # file of the same format. The word "change" could be in any row. Keep that in mind.
    # Don't forget to skip/ignore the first two rows and the third column.
    for i in range(4,sh1.nrows):                   # *****
        if sh1.cell(i,3).value == "change":
            for b in int(sh1.cell(i,1).value):
                calc_per = numpy.irr(colB) * 100
                if calc_per < given_per:
                    for i in range(4,sh1.nrows):
                        if sh1.cell(i,3).value == "change":
                            for b in sh1.cell(i,1).value:
                                b+1;
                else:
                    break                          # *****
    # Remember to use the formula to check the percentages.
    #print("With adjusted values, the percentages are now equal,"
    #      "or calc_per is greater than given_per.")

请仔细阅读评论以了解代码。我正在努力的部分是从#*****开始和结束的行。

我们在这个赋值中需要做的是使用公式函数numpy.irr将“给定百分比”值等于10%,如代码第24行所示。使用B列中的当前值,我们只获得5%,因此我们需要调整B6和B8中的值,这样当我们再次使用numpy.irr时,我们得到的百分比值为10%或更高。基本上,我们需要calc_per&gt; = given_per。

当我使用excel求解器使calc_per达到10%时,B6和B8中的新值分别为1661.45和434.39。显然,它们可能是任何东西。这就是excel所产生的。

excel文件的片段如下图所示:

enter image description here

谢谢!

0 个答案:

没有答案