Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Simon-Stone
Copy link

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

@github-actions
Copy link

github-actions bot commented Jun 7, 2023

🆗 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.

@martinosorb
Copy link
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?

@tobyhodges
Copy link
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
```

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants