尝试减去列表元素的百分比

时间:2019-07-15 02:35:05

标签: python python-3.x data-science

我正在尝试从利润列表中的每个索引的i号中减去30%(税),但是在运行代码时,我会抛出类型错误:

TypeError:无法将序列乘以'float'类型的非整数

我试图将每个单独的元素提取到浮动变量中,但是没有运气,将不胜感激。

我试图将每个单独的元素提取到浮动变量中,但是没有运气,将不胜感激。

我本来在代码内部乘以0.70,但是为了清楚起见,我将其移至变量。

for i in range(len(revenue)):
    profit[i] = [
        revenue[i] - expenses[i]
    ]
print("The profit for each month is:")
print(profit)
The profit for each month is:
[[2522.67], [1911.3900000000003], [-3707.790000000001], [-2914.3099999999995], [-599.9200000000001], [7265.24], [8210.550000000001], [3944.9700000000003], [3328.3899999999994], [-2238.6500000000015], [659.5999999999985], [11629.54]]

percentage = 0.7
for i in range(len(profit)):
    tax[i] = [
        profit[i] * percentage
    ]
print("The profit after tax for each month is:")
print(tax)

我希望获得一个新列表,其值是利润列表中每个列表的30%,相反,我会抛出此错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-68-644d71aa4363> in <module>
      2 for i in range(len(profit)):
      3     tax[i] = [
----> 4         profit[i] * percentage
      5     ]
      6 print("The profit after tax for each month is:")

TypeError: can't multiply sequence by non-int of type 'float'

0 个答案:

没有答案