# how to sum sums

I am new to sagemath and I am trying to calculate some formulas using it.

I learned how to write a sum such as

```
sage: a = [2, 2, 3, 4]
sage: d = [2, 4, 5, 6]
sage: def pi(i):
....: var("i")
....: f = sum([a[k]*d[k] for k in range(i + 1)])
....: return f
```

However, I am having difficulties to write a sum for `pi(i)`

such as

```
sage: def Z(a, b):
....: var("a b")
....: l = len(d) - 1
....: def pi(i):
....: var("i")
....: f = sum([a[k]*d[k] for k in range(i + 1)])
....: return f
....: h = sum([pi(i) for i in range(l - 1)])
....: return h
```

but I got error. I tried to use

```
for i in range(l - 1):
```

also it doesn't work.

As a first step, redefining new (local) symbolic variable(s) with the same name(s) as your function parameters forbids access to the latter, hence your errors...

in other words, remove var from inside your functions