while j <= n:
Should be
while j < n;
range(5) == [0, 1, 2, 3, 4]. There is no 5 element (but there are five elements, because there's a 0)
But this isn't the pythonic approach. Iterate over the list instead of a range the size of the list:
def oneVolume(n):
Sum = 0
nList = []
for i in range(n):
nList.append(2*random()-1)
for j in nList:
Sum = Sum + j
return Sum
But you can go a whole lot further in simplifying, using a generator and the built-in sum function:
def oneVolume(n):
return sum(2*random()-1 for _ in range(n))
The generator is a lot like a list, [2*random()-1 for _ in range(n)], only more transient. That gets passed to the sum, which automatically does what you intended by the second half of your function.
sumnList? Trysum(nList).sum([x**2 for x in nList])sum(map(lambda x: x**2, nList))