Fix issue #204: Remove a poorly designed challenge#651
Open
Simon-Stone wants to merge 1 commit intoswcarpentry:mainfrom
Open
Fix issue #204: Remove a poorly designed challenge#651Simon-Stone wants to merge 1 commit intoswcarpentry:mainfrom
Simon-Stone wants to merge 1 commit intoswcarpentry:mainfrom
Conversation
🆗 Pre-flight checks passed 😃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. |
Contributor
|
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? |
Member
|
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
```
1. 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 | | | |
1. **Before running this code block,** think about what you expect the result to be.
Can you identify which of the options below is correct?
1. A `NameError` again
1. ```output
adjusted: 0.01238691049085659
temperature after call: 294.15
```
1. ```output
adjusted: 0.01238691049085659
temperature after call: 0.01238691049085659
```
1. Now run the code. Did you predict the result correctly?
::::::::::::::: 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:
```output
adjusted: 0.01238691049085659
temperature after call: 294.15
```
:::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes issue #204 by simply removing the challenge in question.