Menu

[Solved]Write Function Called Sum Row Compute Sum Numbers One Row 2d List Function Take 2d List Ro Q37125677

Write a function called sum row that can compute the sum of all the numbers in one row of a 2D list. The function should take#initially empty 2D list grid-2D-list [] = #loop through each line of the file ine in grid file lines: #strip the newline chareturn grid 2D_list main() Console 1 Enter a file name 2 magic_square.txt 3 Here are the sums of each row: 4 15 5 15 6 15 Inp

Write a function called sum row that can compute the sum of all the numbers in one row of a 2D list. The function should take the 2D list and a row number as parameters. The function should return the sum. Use the following starter code: def main() print ( ‘Enter a file name) user_filename-input grid – read number_grid(user filename) num rowslen (grid) print(‘Here are the sums of each row:) for n in range (num rows): print sum_row (grid,n) #paran: filename – the name of a file #returns : the 2D list of numbers from the file #This function reads the comma-separated numbers # from a file into a 2D list of integers def read number grid(filename): #open the file, read the lines to a list, #then close it grid file-open (filename, ‘r) grid file_lines-gridfile.readlines() grid file.close() Questions? Ask Us #initially empty 2D list grid-2D-list [] = #loop through each line of the file ine in grid file lines: #strip the newline character line line.rstrip) #split the line into values based #on the commas row-line.split(“” #loop through each element in the #new row and convert it to an int num_entries-len(row) for idx in range (num entries): row [ idx ] int ( row [ idx ] ) = #append the new row onto the 2D list grid 2D_list.append (row) return grid 2D list return grid 2D_list main() Console 1 Enter a file name 2 magic_square.txt 3 Here are the sums of each row: 4 15 5 15 6 15 Input Files magic_square.txt Show transcribed image text Write a function called sum row that can compute the sum of all the numbers in one row of a 2D list. The function should take the 2D list and a row number as parameters. The function should return the sum. Use the following starter code: def main() print ( ‘Enter a file name) user_filename-input grid – read number_grid(user filename) num rowslen (grid) print(‘Here are the sums of each row:) for n in range (num rows): print sum_row (grid,n) #paran: filename – the name of a file #returns : the 2D list of numbers from the file #This function reads the comma-separated numbers # from a file into a 2D list of integers def read number grid(filename): #open the file, read the lines to a list, #then close it grid file-open (filename, ‘r) grid file_lines-gridfile.readlines() grid file.close() Questions? Ask Us
#initially empty 2D list grid-2D-list [] = #loop through each line of the file ine in grid file lines: #strip the newline character line line.rstrip) #split the line into values based #on the commas row-line.split(“” #loop through each element in the #new row and convert it to an int num_entries-len(row) for idx in range (num entries): row [ idx ] int ( row [ idx ] ) = #append the new row onto the 2D list grid 2D_list.append (row) return grid 2D list
return grid 2D_list main() Console 1 Enter a file name 2 magic_square.txt 3 Here are the sums of each row: 4 15 5 15 6 15 Input Files magic_square.txt

Expert Answer


Answer to Write a function called sum row that can compute the sum of all the numbers in one row of a 2D list. The function should… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *