CS.py
CS.py

Reputation: 43

Multiply a value of a list with the previous value

Here's the code that i'm using:

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)

and here's the error that i'm getting:

 TypeError: list indices must be integers or slices, not float

Upvotes: 1

Views: 1058

Answers (1)

Sercan
Sercan

Reputation: 2169

You have added float numbers in pie list:

result = (i - step) / 100
    pie.append(result)  # [0.0, 0.01, 0.02, ...]

Then you used those numbers here as index:

for i in pie:
    result = pie[1] * pie[i - 1]
    pie.append(result)

Error is also saying same thing: list indices must be integers or slices, not float

Upvotes: 1

Related Questions