swcarpentry / python-novice-gapminder

Plotting and Programming in Python
http://swcarpentry.github.io/python-novice-gapminder/
Other
162 stars 428 forks source link

Fix issue #204: Remove a poorly designed challenge #651

Open Simon-Stone opened 1 year ago

Simon-Stone commented 1 year ago

Fixes issue #204 by simply removing the challenge in question.

github-actions[bot] commented 1 year ago

:ok: Pre-flight checks passed :smiley:

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.

martinosorb commented 1 year ago

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?

tobyhodges commented 1 year ago

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
  2. 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
    2. adjusted: 0.01238691049085659
      temperature after call: 294.15
    3. adjusted: 0.01238691049085659
      temperature after call: 0.01238691049085659
  3. 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:

adjusted: 0.01238691049085659
temperature after call: 294.15

:::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::