|
| 1 | +<?xml version="1.0"?> |
| 2 | +<worksheet xml:id="flal_list_groupsub" groupwork="yes" groupsize="4" label="funcWithListsAmdLoops"> |
| 3 | + <title>Group Work: Functions with Lists and Loops</title> |
| 4 | + <p>It is best to use a POGIL approach with the following. In POGIL students work |
| 5 | + in groups on activities and each member has an assigned role. For more information see <url href="https://cspogil.org/Home" visual="https://cspogil.org/Home">https://cspogil.org/Home</url>.</p> |
| 6 | + <note> |
| 7 | + <p>If you work in a group, have only one member of the group fill in the answers on this page. You will be able to share your answers with the group at the bottom of the page.</p> |
| 8 | + </note> |
| 9 | +<section xml:id="flal_group-work-functions-with-lists-and-loops"> |
| 10 | + <title>Learning Objectives</title> |
| 11 | + <introduction> |
| 12 | + <p>Students will know and be able to do the following.</p> |
| 13 | + <p> |
| 14 | + <em>Content Objectives:</em> |
| 15 | + </p> |
| 16 | + <p> |
| 17 | + <ul> |
| 18 | + <li> |
| 19 | + <p>Understand how list indexing and slicing works.</p> |
| 20 | + </li> |
| 21 | + <li> |
| 22 | + <p>Gain famililarity with built-in fuctions that take lists as well as list methods.</p> |
| 23 | + </li> |
| 24 | + <li> |
| 25 | + <p>Recognize a for-each loop.</p> |
| 26 | + </li> |
| 27 | + <li> |
| 28 | + <p>Recognize a range.</p> |
| 29 | + </li> |
| 30 | + <li> |
| 31 | + <p>Recognize a for loop.</p> |
| 32 | + </li> |
| 33 | + <li> |
| 34 | + <p>Recognize a while loop.</p> |
| 35 | + </li> |
| 36 | + </ul> |
| 37 | + </p> |
| 38 | + <p> |
| 39 | + <em>Process Objectives:</em> |
| 40 | + </p> |
| 41 | + <p> |
| 42 | + <ul> |
| 43 | + <li> |
| 44 | + <p>Predict the output of code with lists (Information Processing)</p> |
| 45 | + </li> |
| 46 | + <li> |
| 47 | + <p>Write code using the slice operator (Assessment)</p> |
| 48 | + </li> |
| 49 | + <li> |
| 50 | + <p>Write code using built-in functions that take lists</p> |
| 51 | + </li> |
| 52 | + <li> |
| 53 | + <p>Predict the outcome of code with lists and loops.</p> |
| 54 | + </li> |
| 55 | + <li> |
| 56 | + <p>Modify code with loops.</p> |
| 57 | + </li> |
| 58 | + <li> |
| 59 | + <p>Fix code that processes a list with loops.</p> |
| 60 | + </li> |
| 61 | + </ul> |
| 62 | + </p> |
| 63 | +</introduction> |
| 64 | + <subsection xml:id="flal_the-for-each-loop"> |
| 65 | + <title>The For-Each Loop</title> |
| 66 | + <p>A for-each loop in Python will loop though the items in a list starting with the item at index 0, then index 1, and so on till the last item in the list.</p> |
| 67 | + <exercise label="funct_ll_fitb_count_odd_first"> |
| 68 | + <statement> |
| 69 | + <p>What is the first thing that will be printed by the code below? <var/> </p> |
| 70 | + </statement> |
| 71 | + <setup> |
| 72 | + <var> |
| 73 | + <condition number="1"> |
| 74 | + <feedback> |
| 75 | + <p>It will print the number of values that are odd in the first list, which is one.</p> |
| 76 | + </feedback> |
| 77 | + </condition> |
| 78 | + <condition string=".*"> |
| 79 | + <feedback> |
| 80 | + <p>Run the code to check.</p> |
| 81 | + </feedback> |
| 82 | + </condition> |
| 83 | + </var> |
| 84 | + </setup> |
| 85 | + </exercise> |
| 86 | + <exercise label="flal_ll_fitb_count_odd_last"> |
| 87 | + <statement> |
| 88 | + <p>What is the last thing that will be printed by the code below? <var/> </p> |
| 89 | + </statement> |
| 90 | + <setup> |
| 91 | + <var> |
| 92 | + <condition number="0"> |
| 93 | + <feedback> |
| 94 | + <p>It will print the number of values that are odd in the last list which is zero.</p> |
| 95 | + </feedback> |
| 96 | + </condition> |
| 97 | + <condition string=".*"> |
| 98 | + <feedback> |
| 99 | + <p>Run the code to check.</p> |
| 100 | + </feedback> |
| 101 | + </condition> |
| 102 | + </var> |
| 103 | + </setup> |
| 104 | + </exercise> |
| 105 | + <exercise label="flal_ll_ac_count_odd"> |
| 106 | + <statement> |
| 107 | + <p>Run this code to see what it prints.</p> |
| 108 | + </statement> |
| 109 | + <program xml:id="flal_ll_ac_count_odd_editor" interactive="activecode" language="python"> |
| 110 | + <input> |
| 111 | +# function definition |
| 112 | +def count_odd(num_list): |
| 113 | + count = 0 |
| 114 | + for num in num_list: |
| 115 | + if num % 2 == 1: |
| 116 | + count += 1 |
| 117 | + return count |
| 118 | + |
| 119 | +# function definition |
| 120 | +def main(): |
| 121 | + list1 = [2, 8, 9] |
| 122 | + print(count_odd(list1)) |
| 123 | + list1 = [1, 3, 5] |
| 124 | + print(count_odd(list1)) |
| 125 | + list1 = [0] |
| 126 | + print(count_odd(list1)) |
| 127 | + |
| 128 | +# function call |
| 129 | +main() |
| 130 | + </input> |
| 131 | + </program> |
| 132 | + </exercise> |
| 133 | + </subsection> |
| 134 | + |
| 135 | + <subsection xml:id="flal_range-and-for"> |
| 136 | + <title>Range and For</title> |
| 137 | + <p>How do you loop just a set number of times? You can use |
| 138 | + the built-in <c>range</c> function to do this.</p> |
| 139 | + |
| 140 | + <exercise label="flal_ll_fitb_print_to_last"> |
| 141 | + <statement> |
| 142 | + <p>What is last value that that following code prints? <var/> </p> |
| 143 | + </statement> |
| 144 | + <setup> |
| 145 | + <var> |
| 146 | + <condition number="2"> |
| 147 | + <feedback> |
| 148 | + <p>The code prints from 0 to the passed end (exclusive). The end is 3 which means the last value is 2.</p> |
| 149 | + </feedback> |
| 150 | + </condition> |
| 151 | + <condition string=".*"> |
| 152 | + <feedback> |
| 153 | + <p>What is the parameter in the second call to count_to?</p> |
| 154 | + </feedback> |
| 155 | + </condition> |
| 156 | + </var> |
| 157 | + </setup> |
| 158 | + </exercise> |
| 159 | + <exercise label="flal_ll_ac_count_to"> |
| 160 | + <statement> |
| 161 | + <p>Run this code to see what it prints.</p> |
| 162 | + </statement> |
| 163 | + <program xml:id="flal_ll_ac_count_to_editor" interactive="activecode" language="python"> |
| 164 | + <input> |
| 165 | +# function definition |
| 166 | +def print_to(end): |
| 167 | + for x in range(end): |
| 168 | + print(x) |
| 169 | + |
| 170 | +# function definition |
| 171 | +def main(): |
| 172 | + print_to(5) |
| 173 | + print() |
| 174 | + print_to(3) |
| 175 | + |
| 176 | +# function call |
| 177 | +main() |
| 178 | + </input> |
| 179 | + </program> |
| 180 | + </exercise> |
| 181 | + |
| 182 | + <note> |
| 183 | + <p>The <c>range(end)</c> function will produce values from 0 to end - 1.</p> |
| 184 | + </note> |
| 185 | + |
| 186 | + <exercise label="flal_ll_fitb_print_from_to_by_last"> |
| 187 | + <statement> |
| 188 | + <p>What is last value that that following code prints? <var/> </p> |
| 189 | + </statement> |
| 190 | + <setup> |
| 191 | + <var> |
| 192 | + <condition number="9"> |
| 193 | + <feedback> |
| 194 | + <p>The code prints from start (inclusive) which is 1 to the passed end (exclusive) which is 10 and changes by 2 each time so the end is 9.</p> |
| 195 | + </feedback> |
| 196 | + </condition> |
| 197 | + <condition string=".*"> |
| 198 | + <feedback> |
| 199 | + <p>Run the code and see.</p> |
| 200 | + </feedback> |
| 201 | + </condition> |
| 202 | + </var> |
| 203 | + </setup> |
| 204 | + </exercise> |
| 205 | + |
| 206 | + <exercise label="flal_ll_ac_print_from_to_by"> |
| 207 | + <statement> |
| 208 | + <p>Run this code to see what it prints.</p> |
| 209 | + </statement> |
| 210 | + <program xml:id="flal_ll_ac_print_from_to_by_editor" interactive="activecode" language="python"> |
| 211 | + <input> |
| 212 | +# function definition |
| 213 | +def print_from_to_by(start, end, by): |
| 214 | + for x in range(start, end, by): |
| 215 | + print(x) |
| 216 | + |
| 217 | +# function definition |
| 218 | +def main(): |
| 219 | + print_from_to_by(10,0,-1) |
| 220 | + print() |
| 221 | + print_from_to_by(1,10,2) |
| 222 | + |
| 223 | +# function call |
| 224 | +main() |
| 225 | + </input> |
| 226 | + </program> |
| 227 | + </exercise> |
| 228 | + |
| 229 | + <note> |
| 230 | + <p>The function range(start, end, by) will return a range object (an iterator) that allows you to loop from start (inclusive) to end (exclusive) and add the value of by after each execution of the loop.</p> |
| 231 | + </note> |
| 232 | + |
| 233 | + <exercise label="flal_ll_pp_total_at_odd_indices" numbered="yes" adaptive="yes" indentation="hide" language="python"> |
| 234 | + <statement> |
| 235 | + <p>Drag the blocks from the left and put them in the correct order on the right to define a function <c>total_at_odd_indices</c> that returns the total of the numbers at odd indices in the passed list.</p> |
| 236 | + </statement> |
| 237 | + <blocks> |
| 238 | + <block order="3"> |
| 239 | + <choice correct="yes"> |
| 240 | + <cline>def total_at_odd_indices(alist):</cline> |
| 241 | + </choice> |
| 242 | + <choice> |
| 243 | + <cline>def total_at_odd_indices(alist) </cline> |
| 244 | + </choice> |
| 245 | + </block> |
| 246 | + <block order="5"> |
| 247 | + <cline> total = 0</cline> |
| 248 | + </block> |
| 249 | + <block order="1"> |
| 250 | + <choice correct="yes"> |
| 251 | + <cline> for i in range(1,len(alist),2):</cline> |
| 252 | + </choice> |
| 253 | + <choice> |
| 254 | + <cline> for i in range(1,len(alist)): </cline> |
| 255 | + </choice> |
| 256 | + </block> |
| 257 | + <block order="2"> |
| 258 | + <choice correct="yes"> |
| 259 | + <cline> total += alist[i]</cline> |
| 260 | + </choice> |
| 261 | + <choice> |
| 262 | + <cline> total += i </cline> |
| 263 | + </choice> |
| 264 | + </block> |
| 265 | + <block order="7"> |
| 266 | + <cline> return total</cline> |
| 267 | + </block> |
| 268 | + </blocks> |
| 269 | + </exercise> |
| 270 | + </subsection> |
| 271 | + |
| 272 | + <subsection xml:id="flal_while-loops"> |
| 273 | + <title>While Loops</title> |
| 274 | + <p>A while loop repeats while a Boolean expression is True.</p> |
| 275 | + <exercise label="flal_ll_ac_inifite"> |
| 276 | + <statement> |
| 277 | + <p>Try running the code below.</p> |
| 278 | + </statement> |
| 279 | + <program xml:id="flal_ll_ac_inifite_editor" interactive="activecode" language="python"> |
| 280 | + <input> |
| 281 | +def example(): |
| 282 | + count = 0 |
| 283 | + while (True): |
| 284 | + print("This is the song that never ends", count) |
| 285 | + count += 1 |
| 286 | + if (count > 100): |
| 287 | + break |
| 288 | + |
| 289 | +def main(): |
| 290 | + example() |
| 291 | + |
| 292 | +main() |
| 293 | + </input> |
| 294 | + </program> |
| 295 | + </exercise> |
| 296 | + |
| 297 | + <exercise label="flal_ll_fitb_while_break"> |
| 298 | + <statement> |
| 299 | + <p>What keyword is used to stop the loop in the above code? <var/> </p> |
| 300 | + </statement> |
| 301 | + <setup> |
| 302 | + <var> |
| 303 | + <condition string="break"> |
| 304 | + <feedback> |
| 305 | + <p>The break keyword will stop the enclosing loop.</p> |
| 306 | + </feedback> |
| 307 | + </condition> |
| 308 | + <condition string=".*"> |
| 309 | + <feedback> |
| 310 | + <p>How does the code stop above?</p> |
| 311 | + </feedback> |
| 312 | + </condition> |
| 313 | + </var> |
| 314 | + </setup> |
| 315 | + </exercise> |
| 316 | + <p>What do you think would happen if you deleted lines 6 and 7 in the above code?</p> |
| 317 | + |
| 318 | + <note> |
| 319 | + <p>A loop that never ends is called an infinite loop. A while loop should have some way to end. If you have an infinite loop you may need to refresh the page to stop it.</p> |
| 320 | + </note> |
| 321 | + |
| 322 | + <p>If you worked in a group, you can copy the answers from this page to the other group members. Select the group members below and click the button to share the answers.</p> |
| 323 | + </subsection> |
| 324 | +</section> |
| 325 | +</worksheet> |
0 commit comments