Skip to content
Snippets Groups Projects
Commit 5a4975d5 authored by sheepmax's avatar sheepmax
Browse files

Basic exercise checking example

parent c2ce207f
No related branches found
No related tags found
2 merge requests!51Blank book,!34Exercise checking
Pipeline #205531 passed
......@@ -66,6 +66,7 @@ parts:
- file: cookbook/benchmarks/benchmark_tags_dont_execute.ipynb
- file: cookbook/benchmarks/benchmark_load_dont_execute.ipynb
- file: cookbook/widgets_dont_execute.ipynb
- file: cookbook/exercise_checking_dont_execute.ipynb
- caption: Old Material
chapters:
- file: old/blank
......
%% Cell type:markdown id: tags:
# Exercise checking with interactive elements
Exercise checking can be intuitively incorporated into Python using tags. These examples show different approaches to achieving this goal.
%% Cell type:code id: tags:thebe-remove-input-init
``` python
%pip install ipywidgets
import ipywidgets as widgets
from IPython.display import display
import operator
def check_answer(variable_name, expected, comparison = operator.eq):
output = widgets.Output()
button = widgets.Button(description="Check answer")
def _inner_check(button):
with output:
if comparison(globals()[variable_name], expected):
output.outputs = [{'name': 'stdout', 'text': 'Correct!', 'output_type': 'stream'}]
else:
output.outputs = [{'name': 'stdout', 'text': 'Incorrect!', 'output_type': 'stream'}]
button.on_click(_inner_check)
display(button, output)
```
%% Cell type:code id: tags:thebe-init
``` python
# This example has the user type in the answer as a Python variable, but they need to run the cell to update the answer checked!
pi = 3.14
```
%% Cell type:code id: tags:thebe-remove-input-init
``` python
import math
check_answer("pi", 3.14, math.isclose)
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment