I have an equation and want to sum it's results with different values. I enter a number using Number = input()
and the equation takes numbers from the input according to an index. The wanted indices are from i = 1 to i = 4.
The equation is : Number[i] + Number[i+1]*3 + i
So for example, if my input is Number = 879463
, then firstly for i = 1
: Number[1] + Number[2]*3 + 1
, which equals to 8 + 7*3 + 1 = 30
Then for i = 2
: Number[2] + Number[3]*3 + 2
.. and so on, until i = 4
.
At the end, sum the results and store in total
.
Here is my code:
Number = input()
total = 0
def equation(i,Number):
x = (Number[i] + Number[i+1]*3 + i)
return x
for i in range(len(Number)):
total += equation(i,Number)
print(total)
For this code I get the error:
IndexError: string index out of range
Am I even in the right direction here ?