An example of the accumulator pattern is given in the following code. Read the docstring aloud. 1 def sum_area(room_widths): 2 ''' room_widths is a list of numeric values 3 The shape of each room is square. 4 5 Returns the area of the rooms. 6 ''' 7 total_area = 0 for in 8 width room_widths: 9 total_area = total_area + width**2 return 10 total_area Enter the code for the function in the code editor and execute it. Then call the function from the IPython session and verify that your result matches the result shown here. In []: rooms = [10, 20, 16, 30] In []: sum_area(rooms) Out[]: 1656 Lines 7, 9, and 10 each refer to the variable Describe how total_area. these lines of code relate to the accumulator pattern. Some of the code needed to define a function is shown number_of_eggs() below. The way to call the function and the output you want are shown further below. Complete the function in the code editor and execute number_of_eggs() it. Call the function from the IPython session. Revise until your output matches the required result. def 1 number_of_eggs(dozens): 2 '''dozens is a list of numeric values 3 4 Returns the total number of eggs for the dozens in the list. 5 ''' # What code runs before the loop starts? 6 for in 7 number dozens: 8 # What code runs each time through the loop? 9 # What code runs after the loop is finished? In []: egg_dozens = [3, 5, 2, 10] In []: number_of_eggs(egg_dozens) Out[]: 240 Read the docstring aloud. Why is the accumulator pattern needed to create this function? What would be a good name for the variable playing the accumulator role? Enter the code in the code editor. Replace the comments on lines 6, 8, and 9 with your own comments written in pseudocode.
An example of the accumulator pattern is given in the following code. Read the docstring aloud. 1 2 3 4 5 6 7 8 9 10 def sum_area(room_widths): ''' room_widths is a list of numeric values The shape of each room is square. Returns the area of the rooms. ''' total_area = 0 for width in room_widths: total_area = total_area + width**2 return total_area Enter the code for the function in the code editor and execute it. Then call the function from the IPython session and verify that your result matches the result shown here. In []: In []: Out[]: rooms = [10, 20, 16, 30] sum_area(rooms) 1656 Lines 7, 9, and 10 each refer to the variable total_area. Describe how these lines of code relate to the accumulator pattern. Some of the code needed to define a function number_of_eggs() is shown below. The way to call the function and the output you want are shown further below. Complete the function number_of_eggs() in the code editor and execute it. Call the function from the IPython session. Revise until your output matches the required result. 1 2 3 4 5 6 7 8 9 def number_of_eggs(dozens): '''dozens is a list of numeric values Returns the total number of eggs for the dozens in the list. ''' # What code runs before the loop starts? for number in dozens: # What code runs each time through the loop? # What code runs after the loop is finished? In []: In []: Out[]: egg_dozens = [3, 5, 2, 10] number_of_eggs(egg_dozens) 240 Read the docstring aloud. Why is the accumulator pattern needed to create this function? What would be a good name for the variable playing the accumulator role? Enter the code in the code editor. Replace the comments on lines 6, 8, and 9 with your own comments written in pseudocode.
2.2.5.A%20LoopingPatterns.docx