Open Simon-Stone opened 1 year ago
This pull request has been checked and contains no modified workflow files, spoofing, or invalid commits.
It should be safe to Approve and Run the workflows that need maintainer approval.
Thank you for taking care of this -- I agree that that exercise is poorly designed. However, I'm a bit hesitant on leaving this episode without any exercise. Would it be feasible to modify it so that e.g. it asks a simple question about the scope of variables in a code example like the one there?
How about this?
::::::::::::::::::::::::::::::::::::::: challenge
## Tracing Global Variables
The code block below is a repeat of the example above,
but with a global variable called `temperature` defined
before the `adjust` function definition.
```python
pressure = 103.9
temperature = 294.15 # checkpoint 1
def adjust(t):
temperature = t * 1.43 / pressure
return temperature # checkpoint 2
print('adjusted:', adjust(0.9))
print('temperature after call:', temperature) # checkpoint 3
Think through the code block line-by-line,
tracing the value of the variables as the program is run.
Fill in the table with the values of variables
called pressure
, temperature
, and t
at each of the checkpoints (marked with comments in the code block).
Write "undefined" if the value of a variable has not yet been set.
Checkpoint | pressure |
temperature |
t |
---|---|---|---|
1 | |||
2 | |||
3 |
NameError
againadjusted: 0.01238691049085659
temperature after call: 294.15
adjusted: 0.01238691049085659
temperature after call: 0.01238691049085659
::::::::::::::: solution
After the global variables are first defined,
their values are not modified by any of the subsequent lines.
No global variable called t
is ever created outside the adjust
function,
so this variable remains undefined at all of the checkpoints:
Checkpoint | pressure |
temperature |
t |
---|---|---|---|
1 | 103.9 | 294.15 | undefined |
2 | 103.9 | 294.15 | undefined |
3 | 103.9 | 294.15 | undefined |
This means that the output produced by the code block is option 2:
adjusted: 0.01238691049085659
temperature after call: 294.15
:::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::
Fixes issue #204 by simply removing the challenge in question.