|
| 1 | +--- |
| 2 | +format: live-html |
| 3 | +--- |
| 4 | + |
| 5 | +<script src='../../src/quiz.js'></script> |
| 6 | + |
| 7 | +# 1.1. Exercises |
| 8 | + |
| 9 | +## Questions on Scoping |
| 10 | + |
| 11 | +```python |
| 12 | +toy = "ball" |
| 13 | + |
| 14 | +def playtime(): |
| 15 | + toy = "truck" |
| 16 | + return toy |
| 17 | + |
| 18 | +toy |
| 19 | +``` |
| 20 | + |
| 21 | +<div id='mcq1'></div> |
| 22 | +<script> |
| 23 | + generateQuiz( |
| 24 | + 'mcq1', |
| 25 | + 'Question 1', |
| 26 | + 'Given the code above, what is the output?', |
| 27 | + { |
| 28 | + '<code>ball</code>': 'Well done!', |
| 29 | + '<code>truck</code>': 'Since <code>toy</code> is being called in the global environment, the output of <code>toy</code> is the value of the global variable value of <code>toy</code>.', |
| 30 | + '<code>Error</code>': 'This will still run without an error!', |
| 31 | + }, |
| 32 | + '<code>ball</code>', |
| 33 | + ); |
| 34 | +</script> |
| 35 | + |
| 36 | +```python |
| 37 | +toy = "ball" |
| 38 | + |
| 39 | +def playtime(): |
| 40 | + toy = "truck" |
| 41 | + return toy |
| 42 | + |
| 43 | +playtime() |
| 44 | +``` |
| 45 | + |
| 46 | +<div id='mcq2'></div> |
| 47 | +<script> |
| 48 | + generateQuiz( |
| 49 | + 'mcq2', |
| 50 | + 'Question 2', |
| 51 | + 'Given the code above, what is the output?', |
| 52 | + { |
| 53 | + '<code>ball</code>': '<code>toy</code> is equal to <code>"truck"</code> in the local environment so it will return the local variable value of <code>toy</code>.', |
| 54 | + '<code>truck</code>': '', |
| 55 | + '<code>Error</code>': 'This will still run without an error!', |
| 56 | + }, |
| 57 | + '<code>truck</code>', |
| 58 | + ); |
| 59 | +</script> |
| 60 | + |
| 61 | +```python |
| 62 | +def playtime(): |
| 63 | + toy = "truck" |
| 64 | + return toy |
| 65 | + |
| 66 | +toy |
| 67 | +``` |
| 68 | + |
| 69 | +<div id='mcq3'></div> |
| 70 | +<script> |
| 71 | + generateQuiz( |
| 72 | + 'mcq3', |
| 73 | + 'Question 3', |
| 74 | + 'Given the code above, what is the output?', |
| 75 | + { |
| 76 | + '<code>None</code>': '<code>toy</code> is a local variable, will it be recognized in a global environment?', |
| 77 | + '<code>truck</code>': '<code>toy</code> is a local variable, will it be recognized in a global environment?', |
| 78 | + '<code>Error</code>': 'Great! <code>toy</code> is a local variable, and will not be recognized in the global environment.', |
| 79 | + }, |
| 80 | + '<code>Error</code>', |
| 81 | + ); |
| 82 | +</script> |
| 83 | + |
| 84 | + |
| 85 | +## Side Effects |
| 86 | + |
| 87 | +<div id='mcq4'></div> |
| 88 | +<script> |
| 89 | + generateQuiz( |
| 90 | + 'mcq4', |
| 91 | + 'True or False', |
| 92 | + 'Side effects make your code easier to debug.', |
| 93 | + { |
| 94 | + 'True': 'Ahh! Please go back and read over the slides. Side effects are generally avoided.', |
| 95 | + 'False': 'Well done. This is an important one to get right!', |
| 96 | + }, |
| 97 | + 'False', |
| 98 | + ); |
| 99 | +</script> |
| 100 | + |
| 101 | +A. |
| 102 | + |
| 103 | +```python |
| 104 | +toy = "ball" |
| 105 | + |
| 106 | +def playtime(): |
| 107 | + toy = "truck" |
| 108 | + print(toy) |
| 109 | + |
| 110 | +playtime() |
| 111 | +``` |
| 112 | + |
| 113 | +B. |
| 114 | + |
| 115 | +```python |
| 116 | + |
| 117 | +toy = "ball" |
| 118 | + |
| 119 | +def playtime(): |
| 120 | + toy = "truck" |
| 121 | + return toy |
| 122 | + |
| 123 | +playtime() |
| 124 | +``` |
| 125 | + |
| 126 | +<div id='mcq5'></div> |
| 127 | +<script> |
| 128 | + generateQuiz( |
| 129 | + 'mcq5', |
| 130 | + 'Question 2', |
| 131 | + 'Which of the above functions produce a side effect?', |
| 132 | + { |
| 133 | + 'A': 'Nice job! Printing <code>toy</code> is considered a function side effect!', |
| 134 | + 'B': 'Having a function <code>return</code> something is not considered necessarily a side effect.', |
| 135 | + 'A and B': 'Having a function <code>return</code> something is not considered necessarily a side effect.', |
| 136 | + 'Neither A or B': 'Printing <code>toy</code> is considered a function side effect!', |
| 137 | + }, |
| 138 | + 'A', |
| 139 | + ); |
| 140 | +</script> |
| 141 | + |
| 142 | +<div id='mcq6'></div> |
| 143 | +<script> |
| 144 | + generateQuiz( |
| 145 | + 'mcq6', |
| 146 | + 'True or False', |
| 147 | + 'If a function modifies a variable in the function’s local environment, that is considered a side effect.', |
| 148 | + { |
| 149 | + 'True': 'This is not considered a side effect if a variable created within a function is modified within the same function.', |
| 150 | + 'False': '', |
| 151 | + }, |
| 152 | + 'False', |
| 153 | + ); |
| 154 | +</script> |
| 155 | + |
| 156 | + |
| 157 | +## Writing Functions Without Side Effects |
| 158 | + |
| 159 | +**Instructions:** |
| 160 | +Running a coding exercise for the first time could take a bit of time for everything to load. Be patient, it could take a few minutes. |
| 161 | + |
| 162 | +**When you see `____` in a coding exercise, replace it with what you assume to be the correct code. Run it and see if you obtain the desired output. Submit your code to validate if you were correct.** |
| 163 | + |
| 164 | +_**Make sure you remove the hash (`#`) symbol in the coding portions of this question. We have commented them so that the line won't execute and you can test your code after each step.**_ |
| 165 | + |
| 166 | +The function `kg_to_lb()` is used to convert a list of elements with kg units into a list of elements with lbs units. Unfortunate this function includes a side effect that edits one of the global variables. |
| 167 | + |
| 168 | +```{pyodide} |
| 169 | +weight_kg = [90, 41, 65, 76, 54, 88] |
| 170 | +weight_lb = list() |
| 171 | +
|
| 172 | +def kg_to_lb(weight_list): |
| 173 | + conversion = 2.20462 |
| 174 | + for kg in weight_kg: |
| 175 | + weight_lb.append(kg * conversion) |
| 176 | + return |
| 177 | +
|
| 178 | +kg_to_lb(weight_kg) |
| 179 | +weight_lb |
| 180 | +``` |
| 181 | + |
| 182 | +**Tasks:** |
| 183 | + |
| 184 | +- Write a new function named `better_kg_to_lb` that no longer contains a side effect. |
| 185 | +- Test your new function on the `weight_kg` list and save the results in an object named `weight_lb_again`. |
| 186 | + |
| 187 | +```{pyodide} |
| 188 | +#| setup: true |
| 189 | +#| exercise: writing_functions_without_side_effects |
| 190 | +import pandas as pd |
| 191 | +``` |
| 192 | + |
| 193 | +```{pyodide} |
| 194 | +#| exercise: writing_functions_without_side_effects |
| 195 | +weight_kg = [90, 41, 65, 76, 54, 88] |
| 196 | +
|
| 197 | +# rewrite the code and function above so that it does not have any side effects |
| 198 | +____ ____(____): |
| 199 | + ____ |
| 200 | + ____ |
| 201 | + ____ |
| 202 | + ____ |
| 203 | +
|
| 204 | +# Test your new function on weight_kg and name the new object weight_lb_again |
| 205 | +____ = ____ |
| 206 | +____ |
| 207 | +``` |
| 208 | + |
| 209 | +```{pyodide} |
| 210 | +#| exercise: writing_functions_without_side_effects |
| 211 | +#| check: true |
| 212 | +from src.utils import print_correct_msg |
| 213 | +
|
| 214 | +assert isinstance(result, list), "Your function should return a list." |
| 215 | +assert len(result) == 6, "Your function output is incorrect. Are you getting rid of side effects?" |
| 216 | +assert round(sum(result), 2) == 912.71, "Your function output is incorrect. Check your calculation." |
| 217 | +print_correct_msg() |
| 218 | +``` |
| 219 | + |
| 220 | +:::: { .hint exercise="writing_functions_without_side_effects"} |
| 221 | +::: { .callout-note collapse="false"} |
| 222 | + |
| 223 | +## Hint 1 |
| 224 | + |
| 225 | +- Are you putting `weight_lb` inside the function now? |
| 226 | +- Are you returning `weight_lb`? |
| 227 | + |
| 228 | +::: |
| 229 | +:::: |
| 230 | + |
| 231 | +:::: { .solution exercise="writing_functions_without_side_effects" } |
| 232 | +::: { .callout-tip collapse="false"} |
| 233 | + |
| 234 | +## Fully worked solution: |
| 235 | + |
| 236 | +```{pyodide} |
| 237 | +weight_kg = [90, 41, 65, 76, 54, 88] |
| 238 | +
|
| 239 | +# rewrite the code and function above so that it does not have any side effects |
| 240 | +def better_kg_to_lb(weight_list): |
| 241 | + weight_lb = list() |
| 242 | + conversion = 2.20462 |
| 243 | + |
| 244 | + for kg in weight_list: |
| 245 | + weight_lb.append(kg * conversion) |
| 246 | + return weight_lb |
| 247 | +
|
| 248 | +# Test your new function on weight_kg and name the new object weight_lb_again |
| 249 | +weight_lb_again = better_kg_to_lb(weight_kg) |
| 250 | +weight_lb_again |
| 251 | +``` |
| 252 | + |
| 253 | +::: |
| 254 | +:::: |
0 commit comments