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

2024-06-08 14:27:07 发布

您现在位置:Python中文网/ 问答频道 /正文

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.")

请通读注释以理解代码。我正在挣扎的部分是从#*****开始到结束的线条。你知道吗

在这个赋值中,我们需要做的是用公式函数等于10%的“给定百分比”值numpy.irr公司如代码第24行所示。对于B列中的当前值,我们只得到5%,因此我们需要调整B6和B8中的值,以便在使用numpy.irr公司同样,我们得到的百分比值是10%或更高。基本上,我们需要calc\u per>;=给定的\u per。你知道吗

当我用excel解算器得到10%的计算值时,B6和B8中的新值分别是1661.45和434.39。很明显,它们可以是任何东西。这就是excel产生的结果。你知道吗

excel文件的片段如下所示:

enter image description here

谢谢!你知道吗


Tags: theinnumpyforvaluecellcalcchange