Python 将列表的值与上一个值相乘
以下是我正在使用的代码:Python 将列表的值与上一个值相乘,python,list,Python,List,以下是我正在使用的代码: pie = [] start = 102 stop = 1 step = 1 for i in range(stop,start): result = (i - step) / 100 pie.append(result) print(pie) for i in pie: result = pie[i] * pie[i-1] pie.append(result) print(pie) 下面是我得到的错误: TypeError: list ind
pie = []
start = 102
stop = 1
step = 1
for i in range(stop,start):
result = (i - step) / 100
pie.append(result)
print(pie)
for i in pie:
result = pie[i] * pie[i-1]
pie.append(result)
print(pie)
下面是我得到的错误:
TypeError: list indices must be integers or slices, not float
您已在
饼图
列表中添加了浮点数:
result = (i - step) / 100
pie.append(result) # [0.0, 0.01, 0.02, ...]
然后将这些数字用作索引:
for i in pie:
result = pie[1] * pie[i - 1]
pie.append(result)
错误也在说同样的事情:
列表索引必须是整数或片,而不是浮点
将其更改为结果=(i-step)/100
此结果=(i-step)//100
namepie
你没有定义列表pie
,但是你在添加元素,你可能会进入一个无休止的循环,因为你修改了你正在迭代的iterable,在迭代过程中使它变得越来越长,这样你就永远不会到达它的终点……我希望浮动保持不变