mirror of
https://github.com/TeachBooks/manual.git
synced 2026-11-13 23:05:51 +00:00
“moved_stuff_from_mude_cookbook_to_jupiter_book_manual”
This commit is contained in:
parent
4a720f376b
commit
9b04ab332e
17 changed files with 2539 additions and 200 deletions
120
book/features/exercise_checking.ipynb
Normal file
120
book/features/exercise_checking.ipynb
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Exercise checking using check-answer button\n",
|
||||
"\n",
|
||||
"Using the python-enabled interactivity, exercise checking can be intuitively incorporated using a widgets which checks the value of certain variables with respect to the answer. These examples show different approaches to achieving this goal. \n",
|
||||
"\n",
|
||||
"A function has been made to check the value of a variable. As input values it takes the variable name, the expected value (correct answer) and how these values should be compared. You need to add this function to each page, including the imports, and add the `thebe-remove-input-init` tag to the cell.\n",
|
||||
"```python\n",
|
||||
"import micropip\n",
|
||||
"await micropip.install(\"ipywidgets\")\n",
|
||||
"import ipywidgets as widgets\n",
|
||||
"from IPython.display import display\n",
|
||||
"import operator\n",
|
||||
"\n",
|
||||
"def check_answer(variable_name, expected, comparison = operator.eq):\n",
|
||||
" output = widgets.Output()\n",
|
||||
" button = widgets.Button(description=\"Check answer\")\n",
|
||||
" def _inner_check(button):\n",
|
||||
" with output:\n",
|
||||
" if comparison(globals()[variable_name], expected):\n",
|
||||
" output.outputs = [{'name': 'stdout', 'text': 'Correct!', 'output_type': 'stream'}]\n",
|
||||
" else:\n",
|
||||
" output.outputs = [{'name': 'stdout', 'text': 'Incorrect!', 'output_type': 'stream'}]\n",
|
||||
" button.on_click(_inner_check)\n",
|
||||
" display(button, output)\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"To check a float value, you could use `math.isclose` as a function. For checking the equivalence of arrays, you might consider `numpy.array_equiv`\n",
|
||||
"\n",
|
||||
"You can choose to include the correct answers in the same notebook. If students download the page and you didn't specify a custom url, they can find the correct answers. You can also place the correct answers in a seperate .py file which you import in the notebook.\n",
|
||||
"\n",
|
||||
"## Example"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"tags": [
|
||||
"thebe-remove-input-init"
|
||||
]
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import micropip\n",
|
||||
"await micropip.install(\"ipywidgets\")\n",
|
||||
"import ipywidgets as widgets\n",
|
||||
"from IPython.display import display\n",
|
||||
"import operator\n",
|
||||
"\n",
|
||||
"def check_answer(variable_name, expected, comparison = operator.eq):\n",
|
||||
" output = widgets.Output()\n",
|
||||
" button = widgets.Button(description=\"Check answer\")\n",
|
||||
" def _inner_check(button):\n",
|
||||
" with output:\n",
|
||||
" if comparison(globals()[variable_name], expected):\n",
|
||||
" output.outputs = [{'name': 'stdout', 'text': 'Correct!', 'output_type': 'stream'}]\n",
|
||||
" else:\n",
|
||||
" output.outputs = [{'name': 'stdout', 'text': 'Incorrect!', 'output_type': 'stream'}]\n",
|
||||
" button.on_click(_inner_check)\n",
|
||||
" display(button, output)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"tags": [
|
||||
"thebe-init",
|
||||
"auto-execute-page"
|
||||
]
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# 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!\n",
|
||||
"pi = 3.14"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"tags": [
|
||||
"thebe-remove-input-init"
|
||||
]
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import math\n",
|
||||
"check_answer(\"pi\", 3.14, math.isclose)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "base",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.6"
|
||||
},
|
||||
"orig_nbformat": 4
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue