Skip to content

Commit 57376d8

Browse files
authored
Merge pull request #167 from pryn-kb/main
x
2 parents a2696a3 + 2e40ae7 commit 57376d8

30 files changed

+592
-235
lines changed
0 Bytes
Binary file not shown.
1.39 KB
Binary file not shown.
0 Bytes
Binary file not shown.
10.5 KB
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
1.87 KB
Binary file not shown.

_build/html/_sources/docs/101/07_conditionals.ipynb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,7 @@
12401240
"tags": []
12411241
},
12421242
"source": [
1243+
"(number-guessing-game)=\n",
12431244
"### <i class=\"fa-solid fa-dice\"></i> Exercise 4: The number guessing game"
12441245
]
12451246
},
@@ -1274,8 +1275,9 @@
12741275
"\n",
12751276
"**Requirements:**\n",
12761277
"\n",
1277-
"Use a `while` loop to repeatedly ask the user for their guess until they guess correctly.\n",
1278-
"Use `if`, `elif`, and `else` statements to provide appropriate hints based on the user’s guess."
1278+
"- 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."
12791281
]
12801282
},
12811283
{
@@ -1365,22 +1367,24 @@
13651367
"# Generate a random secret number between 1 and 20\n",
13661368
"secret_number = random.randint(1, 20)\n",
13671369
"\n",
1368-
"# Initialize variables\n",
1370+
"# Keep track of the number of attempts\n",
13691371
"attempts = 0\n",
1370-
"guessed_correctly = False\n",
1372+
"\n",
1373+
"# Initialise the variable to store the user's guess\n",
1374+
"user_guess = None \n",
13711375
"\n",
13721376
"print(\"Welcome to the Number Guessing Game!\")\n",
13731377
"\n",
1374-
"while not guessed_correctly:\n",
1378+
"# Continue looping until the correct number is guessed\n",
1379+
"while user_guess != secret_number:\n",
13751380
" # Ask the user to guess the secret number\n",
13761381
" user_guess = int(input(\"Guess the secret number (1-20): \"))\n",
1377-
" \n",
1378-
" # Increment the attempts counter\n",
1382+
"\n",
1383+
" # Increase the attempts counter\n",
13791384
" attempts = attempts + 1\n",
13801385
"\n",
1381-
" # Check if the guess is correct, too low, or too high\n",
1386+
" # Compare the guess with the secret number\n",
13821387
" if user_guess == secret_number:\n",
1383-
" guessed_correctly = True\n",
13841388
" print(\"Congratulations! You guessed the correct number in\", attempts, \"attempts.\")\n",
13851389
" elif user_guess < secret_number:\n",
13861390
" print(\"Too low. Try again.\")\n",
@@ -1446,7 +1450,7 @@
14461450
"name": "python",
14471451
"nbconvert_exporter": "python",
14481452
"pygments_lexer": "ipython3",
1449-
"version": "3.12.5"
1453+
"version": "3.13.5"
14501454
}
14511455
},
14521456
"nbformat": 4,

_build/html/_sources/docs/101/99_try_except.ipynb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,73 @@
483483
" print(\"You chose:\", choice)\n"
484484
]
485485
},
486+
{
487+
"cell_type": "markdown",
488+
"id": "8660edde-8b01-4da6-b775-62601df4f3e6",
489+
"metadata": {},
490+
"source": [
491+
"___\n",
492+
"### <i class=\"fa-solid fa-pencil\"></i> Exercise 3: Fixing The number guessing game\n",
493+
"\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",
496+
"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",
497+
"\n",
498+
"**Requirements**\n",
499+
"- Use a `try/except` block around the line where the program converts input to an integer.\n",
500+
"- If the user does not type a valid number, print an error message such as:\n",
501+
" `\"Invalid input. Please enter a number.\"`\n",
502+
"- Make sure the game continues running until the secret number is guessed.\n",
503+
"\n",
504+
"---\n",
505+
"\n",
506+
"````{admonition} Solution\n",
507+
":class: dropdown\n",
508+
"\n",
509+
"```python\n",
510+
"import random\n",
511+
"\n",
512+
"# Generate a random secret number between 1 and 20\n",
513+
"secret_number = random.randint(1, 20)\n",
514+
"\n",
515+
"# Keep track of the number of attempts\n",
516+
"attempts = 0\n",
517+
"\n",
518+
"# Initialise the variable to store the user's guess\n",
519+
"user_guess = None \n",
520+
"\n",
521+
"print(\"Welcome to the Number Guessing Game!\")\n",
522+
"\n",
523+
"# Continue looping until the correct number is guessed\n",
524+
"while user_guess != secret_number:\n",
525+
" try:\n",
526+
" # Ask the user to guess the secret number\n",
527+
" user_guess = int(input(\"Guess the secret number (1-20): \"))\n",
528+
" \n",
529+
" # Increase the attempts counter\n",
530+
" attempts = attempts + 1\n",
531+
"\n",
532+
" # Compare the guess with the secret number\n",
533+
" if user_guess == secret_number:\n",
534+
" print(\"Congratulations! You guessed the correct number in\", attempts, \"attempts.\")\n",
535+
" elif user_guess < secret_number:\n",
536+
" print(\"Too low. Try again.\")\n",
537+
" else:\n",
538+
" print(\"Too high. Try again.\")\n",
539+
"\n",
540+
" except Exception as e:\n",
541+
" # Handle invalid input\n",
542+
" print(\"Invalid input. Please enter a number.\")\n",
543+
"```\n",
544+
"**Explanation:**\n",
545+
"- The line `user_guess = int(input(...))` can raise an exception if the user types something that is not a number.\n",
546+
"- By placing this line inside a `try/except` block, we can catch the error and respond gracefully.\n",
547+
"- The `except Exception as e` block prints a helpful message and the program continues without crashing.\n",
548+
"\n",
549+
"This way, the game only accepts valid integers, but still lets the user try again if they make a mistake.\n",
550+
"````"
551+
]
552+
},
486553
{
487554
"cell_type": "markdown",
488555
"id": "1bdd4b2d-411a-4992-9e81-f6b76921a722",

_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 0x10b943fb0&gt;
471+
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;contextlib.ExitStack at 0x10ba208c0&gt;
472472
</pre></div>
473473
</div>
474474
</div>

0 commit comments

Comments
 (0)