Sum of all arguments ever passed to accumulator Q2. Define a function make_accumulator that returns an accumulator
  function, which takes one numerical argument and returns the sum of
  all arguments ever passed to accumulator. Use a list and not a
  nonlocal statement.


Below is the solution:

def make_accumulator():
    """Return an accumulator function that takes a single numeric argument and
    accumulates that argument into total, then returns total.

    &gt;&gt;&gt; acc = make_accumulator()
    &gt;&gt;&gt; acc(15)
    15
    &gt;&gt;&gt; acc(10)
    25
    &gt;&gt;&gt; acc2 = make_accumulator()
    &gt;&gt;&gt; acc2(7)
    7
    &gt;&gt;&gt; acc3 = acc2
    &gt;&gt;&gt; acc3(6)
    13
    &gt;&gt;&gt; acc2(5)
    18
    &gt;&gt;&gt; acc(4)
    29
    """
    lst = []
    def accumulate(n):
        lst.append(n)
        length = len(lst)
        total = 0
        for index in range(length):
            total = total + lst[index]
        return total
    return accumulate


Can we improve this solution?
An alternative to the closure is to add an attribute to the function, so rather than:

lst = []
def accumulate(n):
    lst.append(n)
    ...


you have:

def accumulate(n):
    accumulate.lst.append(n)
    ...
accumulate.lst = []


It's a little more typing, but one advantage is that it allows easier access to the list so far from outside the function:

&gt;&gt;&gt; accum = make_accumulator()
&gt;&gt;&gt; accum(7)
7
&gt;&gt;&gt; accum(8)
15
&gt;&gt;&gt; accum.lst
[7, 8]


With the closure, this would be accum.func_closure[0].cell_contents.



Using indices is not a very Pythonic way to handle iterating over a list, and x = x + y can be simplified to x += y; compare:

...
length = len(lst)
for index in range(length):
    total = total + lst[index]
...


with:

...
for item in lst:
    total += item
...


There is also sum(lst), which the specification doesn't forbid.




  How can I think of writing pure functional code for above program
  (i.e., without any state change)?


I don't think that this can be achieved in a purely functional way as specified - you are explicitly told to use a list to store the values passed, so there will always be state, and the function you create will give different outputs for the same inputs depending on the previous inputs (no referential transparency).