Skip to content

Commit 06c661d

Browse files
committed
deploy: efe533f
1 parent ee67059 commit 06c661d

File tree

9 files changed

+276
-117
lines changed

9 files changed

+276
-117
lines changed

_sources/docs/101/07_conditionals.ipynb

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,10 +1150,17 @@
11501150
},
11511151
{
11521152
"cell_type": "code",
1153-
"execution_count": 98,
1153+
"execution_count": 1,
11541154
"id": "454db57f-1737-43a5-a778-5d692bdf891c",
11551155
"metadata": {
11561156
"editable": true,
1157+
"execution": {
1158+
"iopub.execute_input": "2025-10-03T18:32:18.665515Z",
1159+
"iopub.status.busy": "2025-10-03T18:32:18.664963Z",
1160+
"iopub.status.idle": "2025-10-03T18:32:21.045722Z",
1161+
"shell.execute_reply": "2025-10-03T18:32:21.044452Z",
1162+
"shell.execute_reply.started": "2025-10-03T18:32:18.665450Z"
1163+
},
11571164
"slideshow": {
11581165
"slide_type": ""
11591166
},
@@ -1167,13 +1174,12 @@
11671174
"name": "stdin",
11681175
"output_type": "stream",
11691176
"text": [
1170-
"Enter 'quit' to exit: exit\n",
11711177
"Enter 'quit' to exit: quit\n"
11721178
]
11731179
}
11741180
],
11751181
"source": [
1176-
"user_input = \"\"\n",
1182+
"user_input = None\n",
11771183
"while user_input != \"quit\":\n",
11781184
" user_input = input(\"Enter 'quit' to exit: \")"
11791185
]
@@ -1191,8 +1197,10 @@
11911197
"source": [
11921198
"```{admonition} Solution\n",
11931199
":class: dropdown\n",
1194-
"This loop continuously asks the user for input until they type “quit.”\n",
1195-
"It checks the value of user_input in each iteration.\n",
1200+
"Before the loop starts, `user_input` is set to `None` to indicate that it has no value yet.\\\n",
1201+
"(`None` is a special value in Python that represents “no value” or “nothing.”)\\\n",
1202+
"This loop continuously asks the user for input until they type “quit.”\\\n",
1203+
"It checks the value of `user_input` in each iteration.\n",
11961204
"````"
11971205
]
11981206
},
@@ -1276,8 +1284,7 @@
12761284
"**Requirements:**\n",
12771285
"\n",
12781286
"- Use a `while` loop to repeatedly ask the user for their guess until they guess correctly.\n",
1279-
"- Use `if`, `elif`, and `else` statements to provide appropriate hints based on the user’s guess.\n",
1280-
"- You can assume that the user will only enter whole numbers (integers); other input does not need to be considered."
1287+
"- Use `if`, `elif`, and `else` statements to provide appropriate hints based on the user’s guess."
12811288
]
12821289
},
12831290
{
@@ -1291,11 +1298,11 @@
12911298
"tags": []
12921299
},
12931300
"source": [
1294-
"````{hint}\n",
1295-
":class: dropdown\n",
1296-
"You will need to use the `input()` function introduced in the exercise above.\n",
1297-
"We expect the users to only type in integers.\n",
1298-
"You can use the `random` module to generate the secret number. Here’s an example of how to import and use it:\n",
1301+
"````{admonition} Input & random generator\n",
1302+
":class: hint\n",
1303+
"- You will need to use the `input()` function introduced in the exercise above.\n",
1304+
"- We expect the users to only type in integers.\n",
1305+
"- You can use the `random` module to generate the secret number. Here’s an example of how to import and use it:\n",
12991306
"\n",
13001307
"```\n",
13011308
"import random\n",
@@ -1360,7 +1367,7 @@
13601367
"source": [
13611368
"````{admonition} Solution\n",
13621369
":class: dropdown\n",
1363-
"This problem can be solved in more than one way - however this is how we would recommend it done to provide clarity and to use the skills you have learned so far.\n",
1370+
"This problem can be solved in more than one way. However, this is how we would recommend it done to provide clarity and to use the skills you have learned so far.\n",
13641371
"```\n",
13651372
"import random\n",
13661373
"\n",
@@ -1391,6 +1398,10 @@
13911398
" else:\n",
13921399
" print(\"Too high. Try again.\")\n",
13931400
"```\n",
1401+
"\n",
1402+
"**Note:** At this stage, the game will crash if the user types something that is not an integer.\\\n",
1403+
"If you want to learn how to handle such cases gracefully, take a look at the chapter on [Error Handling](99_try_except).\n",
1404+
"\n",
13941405
"`````"
13951406
]
13961407
},

_sources/docs/101/99_try_except.ipynb

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,41 @@
491491
"___\n",
492492
"### <i class=\"fa-solid fa-pencil\"></i> Exercise 3: Fixing The number guessing game\n",
493493
"\n",
494-
"In this exercise, you will update {ref}`The number guessing game <number-guessing-game>` so that it can handle invalid input.\\\n",
495-
"At the moment, if the user types something that is not a whole number (integer), the program will crash.\\\n",
494+
"In this exercise, you will update the solution {ref}`the number guessing game <number-guessing-game>` so that it can handle invalid input.\n",
495+
"\n",
496+
"```python\n",
497+
"import random\n",
498+
"\n",
499+
"# Generate a random secret number between 1 and 20\n",
500+
"secret_number = random.randint(1, 20)\n",
501+
"\n",
502+
"# Keep track of the number of attempts\n",
503+
"attempts = 0\n",
504+
"\n",
505+
"# Initialise the variable to store the user's guess\n",
506+
"user_guess = None \n",
507+
"\n",
508+
"print(\"Welcome to the Number Guessing Game!\")\n",
509+
"\n",
510+
"# Continue looping until the correct number is guessed\n",
511+
"while user_guess != secret_number:\n",
512+
" # Ask the user to guess the secret number\n",
513+
" user_guess = int(input(\"Guess the secret number (1-20): \"))\n",
514+
"\n",
515+
" # Increase the attempts counter\n",
516+
" attempts = attempts + 1\n",
517+
"\n",
518+
" # Compare the guess with the secret number\n",
519+
" if user_guess == secret_number:\n",
520+
" print(\"Congratulations! You guessed the correct number in\", attempts, \"attempts.\")\n",
521+
" elif user_guess < secret_number:\n",
522+
" print(\"Too low. Try again.\")\n",
523+
" else:\n",
524+
" print(\"Too high. Try again.\")\n",
525+
"```\n",
526+
"\n",
527+
"At the moment, if the user types something that is not a whole number (integer), the program will crash.\n",
528+
"\n",
496529
"Your task is to place the code that asks for a guess inside a `try/except` block. If the input is not an integer, the program should print a helpful message and allow the user to try again.\n",
497530
"\n",
498531
"**Requirements**\n",
@@ -547,6 +580,47 @@
547580
"- The `except Exception as e` block prints a helpful message and the program continues without crashing.\n",
548581
"\n",
549582
"This way, the game only accepts valid integers, but still lets the user try again if they make a mistake.\n",
583+
"\n",
584+
"An even cleaner solution would be to use a `try/except/else` structure.\\\n",
585+
"In this way, the `try` block only contains the code that might fail, the `except` block handles the error, and the `else` block contains the normal program logic.\\\n",
586+
"This separation can make the code easier to read and maintain.\n",
587+
"\n",
588+
"```python\n",
589+
"import random\n",
590+
"\n",
591+
"# Generate a random secret number between 1 and 20\n",
592+
"secret_number = random.randint(1, 20)\n",
593+
"\n",
594+
"# Keep track of the number of attempts\n",
595+
"attempts = 0\n",
596+
"\n",
597+
"# Initialise the variable to store the user's guess\n",
598+
"user_guess = None \n",
599+
"\n",
600+
"print(\"Welcome to the Number Guessing Game!\")\n",
601+
"\n",
602+
"# Continue looping until the correct number is guessed\n",
603+
"while user_guess != secret_number:\n",
604+
" try:\n",
605+
" # Ask the user to guess the secret number\n",
606+
" user_guess = int(input(\"Guess the secret number (1-20): \"))\n",
607+
" \n",
608+
" except Exception as e:\n",
609+
" # Handle invalid input\n",
610+
" print(\"Invalid input. Please enter a number.\")\n",
611+
" \n",
612+
" else:\n",
613+
" # Increase the attempts counter\n",
614+
" attempts = attempts + 1\n",
615+
"\n",
616+
" # Compare the guess with the secret number\n",
617+
" if user_guess == secret_number:\n",
618+
" print(\"Congratulations! You guessed the correct number in\", attempts, \"attempts.\")\n",
619+
" elif user_guess < secret_number:\n",
620+
" print(\"Too low. Try again.\")\n",
621+
" else:\n",
622+
" print(\"Too high. Try again.\")\n",
623+
"```\n",
550624
"````"
551625
]
552626
},

_to_be_deleted_later/notebooks.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ <h2>Code blocks and outputs<a class="headerlink" href="#code-blocks-and-outputs"
487487
</div>
488488
</div>
489489
<div class="cell_output docutils container">
490-
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;contextlib.ExitStack at 0x7efc4621c440&gt;
490+
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;contextlib.ExitStack at 0x7f923cf47020&gt;
491491
</pre></div>
492492
</div>
493493
</div>

docs/101/07_conditionals.html

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ <h3><i class="fa-solid fa-pencil"></i> Exercise 3: Using the <code class="docuti
969969
What does the function <code class="docutils literal notranslate"><span class="pre">input()</span></code> do?</p>
970970
<div class="cell tag_remove-output tag_hide-output docutils container">
971971
<div class="cell_input docutils container">
972-
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">user_input</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
972+
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">user_input</span> <span class="o">=</span> <span class="kc">None</span>
973973
<span class="k">while</span> <span class="n">user_input</span> <span class="o">!=</span> <span class="s2">&quot;quit&quot;</span><span class="p">:</span>
974974
<span class="n">user_input</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s2">&quot;Enter &#39;quit&#39; to exit: &quot;</span><span class="p">)</span>
975975
</pre></div>
@@ -978,8 +978,10 @@ <h3><i class="fa-solid fa-pencil"></i> Exercise 3: Using the <code class="docuti
978978
</div>
979979
<div class="dropdown admonition">
980980
<p class="admonition-title">Solution</p>
981-
<p>This loop continuously asks the user for input until they type “quit.”
982-
It checks the value of user_input in each iteration.</p>
981+
<p>Before the loop starts, <code class="docutils literal notranslate"><span class="pre">user_input</span></code> is set to <code class="docutils literal notranslate"><span class="pre">None</span></code> to indicate that it has no value yet.<br />
982+
(<code class="docutils literal notranslate"><span class="pre">None</span></code> is a special value in Python that represents “no value” or “nothing.”)<br />
983+
This loop continuously asks the user for input until they type “quit.”<br />
984+
It checks the value of <code class="docutils literal notranslate"><span class="pre">user_input</span></code> in each iteration.</p>
983985
</div>
984986
<div class="attention admonition">
985987
<p class="admonition-title">A note on the input() function</p>
@@ -1013,13 +1015,14 @@ <h3><i class="fa-solid fa-pencil"></i> Exercise 3: Using the <code class="docuti
10131015
<ul class="simple">
10141016
<li><p>Use a <code class="docutils literal notranslate"><span class="pre">while</span></code> loop to repeatedly ask the user for their guess until they guess correctly.</p></li>
10151017
<li><p>Use <code class="docutils literal notranslate"><span class="pre">if</span></code>, <code class="docutils literal notranslate"><span class="pre">elif</span></code>, and <code class="docutils literal notranslate"><span class="pre">else</span></code> statements to provide appropriate hints based on the user’s guess.</p></li>
1016-
<li><p>You can assume that the user will only enter whole numbers (integers); other input does not need to be considered.</p></li>
10171018
</ul>
1018-
<div class="dropdown admonition hint">
1019-
<p class="admonition-title">Hint</p>
1020-
<p>You will need to use the <code class="docutils literal notranslate"><span class="pre">input()</span></code> function introduced in the exercise above.
1021-
We expect the users to only type in integers.
1022-
You can use the <code class="docutils literal notranslate"><span class="pre">random</span></code> module to generate the secret number. Here’s an example of how to import and use it:</p>
1019+
<div class="hint admonition">
1020+
<p class="admonition-title">Input &amp; random generator</p>
1021+
<ul class="simple">
1022+
<li><p>You will need to use the <code class="docutils literal notranslate"><span class="pre">input()</span></code> function introduced in the exercise above.</p></li>
1023+
<li><p>We expect the users to only type in integers.</p></li>
1024+
<li><p>You can use the <code class="docutils literal notranslate"><span class="pre">random</span></code> module to generate the secret number. Here’s an example of how to import and use it:</p></li>
1025+
</ul>
10231026
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">random</span>
10241027
<span class="c1"># Generate a random secret number between 1 and 20</span>
10251028
<span class="n">secret_number</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span>
@@ -1042,7 +1045,7 @@ <h3><i class="fa-solid fa-pencil"></i> Exercise 3: Using the <code class="docuti
10421045
</div>
10431046
<div class="dropdown admonition">
10441047
<p class="admonition-title">Solution</p>
1045-
<p>This problem can be solved in more than one way - however this is how we would recommend it done to provide clarity and to use the skills you have learned so far.</p>
1048+
<p>This problem can be solved in more than one way. However, this is how we would recommend it done to provide clarity and to use the skills you have learned so far.</p>
10461049
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">random</span>
10471050

10481051
<span class="c1"># Generate a random secret number between 1 and 20</span>
@@ -1073,6 +1076,8 @@ <h3><i class="fa-solid fa-pencil"></i> Exercise 3: Using the <code class="docuti
10731076
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Too high. Try again.&quot;</span><span class="p">)</span>
10741077
</pre></div>
10751078
</div>
1079+
<p><strong>Note:</strong> At this stage, the game will crash if the user types something that is not an integer.<br />
1080+
If you want to learn how to handle such cases gracefully, take a look at the chapter on <a class="reference internal" href="99_try_except.html"><span class="doc std std-doc">Error Handling</span></a>.</p>
10761081
</div>
10771082
</section>
10781083
</section>

docs/101/99_sets_and_dictionaries.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ <h3>Removing duplicates from a list<a class="headerlink" href="#removing-duplica
511511
<p class="expanded admonition-title">Hide code cell output</p>
512512
</summary>
513513
<div class="cell_output docutils container">
514-
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[&#39;1984&#39;, &#39;Brave New World&#39;, &#39;Fahrenheit 451&#39;]
514+
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[&#39;Fahrenheit 451&#39;, &#39;Brave New World&#39;, &#39;1984&#39;]
515515
</pre></div>
516516
</div>
517517
</div>
@@ -576,7 +576,7 @@ <h3>Set operations<a class="headerlink" href="#set-operations" title="Link to th
576576
<p class="expanded admonition-title">Hide code cell output</p>
577577
</summary>
578578
<div class="cell_output docutils container">
579-
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>{&#39;banana&#39;, &#39;date&#39;, &#39;apple&#39;, &#39;cherry&#39;}
579+
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>{&#39;banana&#39;, &#39;date&#39;, &#39;cherry&#39;, &#39;apple&#39;}
580580
{&#39;banana&#39;, &#39;cherry&#39;}
581581
{&#39;apple&#39;}
582582
{&#39;date&#39;, &#39;apple&#39;}

0 commit comments

Comments
 (0)