Skip to content

Commit efe533f

Browse files
authored
Merge pull request #168 from pryn-kb/main
links + solutions
2 parents 57376d8 + 1438c9a commit efe533f

29 files changed

+677
-268
lines changed
0 Bytes
Binary file not shown.
3.7 KB
Binary file not shown.
0 Bytes
Binary file not shown.
6.58 KB
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
-976 Bytes
Binary file not shown.

_build/html/_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
},

_build/html/_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
},

_build/html/_to_be_deleted_later/notebooks.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ <h2>Code blocks and outputs<a class="headerlink" href="#code-blocks-and-outputs"
468468
</div>
469469
</div>
470470
<div class="cell_output docutils container">
471-
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;contextlib.ExitStack at 0x10ba208c0&gt;
471+
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;contextlib.ExitStack at 0x10fbf1100&gt;
472472
</pre></div>
473473
</div>
474474
</div>

0 commit comments

Comments
 (0)