def s( d ) : ''' Solves the sigma.s() homework 24 problem description ''' # Nested For Loop Way answer = 0 # Initialize an accumulator to 0 for row in d: # row is each smaller list in d # print(row) for element in row: # Look at each number in each smaller list answer = answer + element # Add each number in each smaller list to answer return answer # You can't use sum on the element in the second for loop because element is a # single element so you don't want to call sum(1) or something on a 1 in a smaller # list. # answer = 0 # for row in d: # Loop through each row (smaller list) # answer = answer + sum( row ) # Add the sum of each smaller list to our answer # return answer answer = 0 for row in d: # Loop throw each smaller list in d row_sum = sum( row ) # Get the sum of the smaller list because we can call sum( list ) answer = answer + row_sum # Add the row sum to our accumulator return answer # Don't name your variables after built in function/keywords! # Don't call variables list or sum or max etc. because those # are keywords! # Remember sum( list ) returns the sum of numbers in a list # answer = sum( d ) # Keep in mind that dataset is a list of lists, not a list of integers! # You can't just call sum( dataset ) - it can't see the numbers # inside this way. # You cannot call sum ( list ) on a single number either! # x = sum(12) will not work! ''' DO NOT MODIFY THE BELOW CODE ''' if ( __name__ == '__main__' ) : import sigma d1 = [ [ 0 ], [ 1, 2 ], [ 1, 2, 3 ], [ 0 ] ] d2 = [ [ 1, 0, 1, 2, 2 ], [ 3, 0, 1, 1, 1, 0 ], [ 2 ], [ 0, 0, 1 ] ] d3 = [ [ 3, 0, 3], [ 3, 0, 3, 0, 1], [ 1, 0, 2 ] ] d4 = [ ] s1 = s( d1 ); print( 's( d1 ) =', s1 ) s2 = sigma.s( d2 ); print( 's( d2 ) =', s2 ) s3 = sigma.s( d3 ); print( 's( d3 ) =', s3 ) s4 = sigma.s( d4 ); print( 's( d4 ) =', s4 )