Here is a small piece of Python code to add elements of a nested list:
def nested_sum(lists):
s = [0,0,0,0,0,0,0,0] # If I delete this line then an error is given
for i in range(len(lists)):
s[i] = sum(lists[i])
print sum(s)
lists = [[1],[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6] ]
nested_sum(lists)
Please excuse the formatting.
Issue with this code lies in the line I have commented.
The error shown was:
NameError: global name 's' is not defined
How can I avoid this error without declaring the list s
before?