|
41 | 41 | "\n",
|
42 | 42 | "# Flow control\n",
|
43 | 43 | "\n",
|
44 |
| - "Not everything happens in order, sometimes decisions must be made and code run, or not run, depending on the result.\n", |
| 44 | + "Not everything happens in order, sometimes decisions must be made and code run, or not run, depending on the result. \n", |
| 45 | + "\n", |
45 | 46 | "<img src=\"Images/flow_control.png\" alt=\"A flowchart about how to handle a lamp not turning on.\" />\n",
|
46 | 47 | "\n",
|
47 | 48 | "We have three key options for conditionals: \n",
|
|
108 | 109 | " guessesTaken = guessesTaken + 1"
|
109 | 110 | ]
|
110 | 111 | },
|
| 112 | + { |
| 113 | + "cell_type": "markdown", |
| 114 | + "metadata": {}, |
| 115 | + "source": [ |
| 116 | + "It's a little bit niche, but a fun fact is that `while` loops can also have an `else` statement attached, which will run only if the loop finishes because the condition is false (so, if you `break` the loop otherwise it won't run)." |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "code", |
| 121 | + "execution_count": 1, |
| 122 | + "metadata": {}, |
| 123 | + "outputs": [ |
| 124 | + { |
| 125 | + "name": "stdout", |
| 126 | + "output_type": "stream", |
| 127 | + "text": [ |
| 128 | + "Hello!\n", |
| 129 | + "Goodbye!\n" |
| 130 | + ] |
| 131 | + } |
| 132 | + ], |
| 133 | + "source": [ |
| 134 | + "x = True\n", |
| 135 | + "while x:\n", |
| 136 | + " print(\"Hello!\")\n", |
| 137 | + " x = False\n", |
| 138 | + "else:\n", |
| 139 | + " print(\"Goodbye!\")" |
| 140 | + ] |
| 141 | + }, |
111 | 142 | {
|
112 | 143 | "cell_type": "markdown",
|
113 | 144 | "metadata": {},
|
|
264 | 295 | "<a id='exercise_5'></a>\n",
|
265 | 296 | "# Exercise 5 -- Guess the number\n",
|
266 | 297 | "\n",
|
267 |
| - "Make a 'guess the number' game, and add any or all of the following features, or even some of your own!\n", |
268 |
| - "- Customise the program\n", |
269 |
| - "- A difficulty setting (limited guesses, wider range of numbers, etc.)\n", |
270 |
| - "- A 2-player version" |
| 298 | + "Make a 'guess the number' game. That meaning you / the user get a certain number of guesses to guess a number between e.g.: 1 and 20. \n", |
| 299 | + "As an extension, add one or more of these features:\n", |
| 300 | + "\n", |
| 301 | + "- Customise the program: e.g.: is the guess too high or too low? \n", |
| 302 | + "- Add difficulty settings. Higher difficulties may give less guesses or a wider range of numbers. \n", |
| 303 | + "- 2-player version: two players try to guess different numbers quicker.\n" |
| 304 | + ] |
| 305 | + }, |
| 306 | + { |
| 307 | + "cell_type": "markdown", |
| 308 | + "metadata": {}, |
| 309 | + "source": [ |
| 310 | + "<details><summary>Ex.5: Step-by-Step</summary>\n", |
| 311 | + "\n", |
| 312 | + "1) First, you'll need a variable storing how many guesses are allowed and how many have been taken. Compare these in a `while` loop, increasing 'guessesTaken' each time.\n", |
| 313 | + "\n", |
| 314 | + "2) Get the user's input, and compare it to the random-integer they need to guess to see if they got it right.\n", |
| 315 | + "\n", |
| 316 | + "For the extension additional customisations:\n", |
| 317 | + "\n", |
| 318 | + "3) Check if the user is guessing too high using the conditions from last session.\n", |
| 319 | + "\n", |
| 320 | + "4) Ask the user for a difficulty (typing an answer from a list you show them), and change the variables used in the `while` and `randint` statements.\n", |
| 321 | + "\n", |
| 322 | + "5) A two-player version will require additional numbers to guess and most likely separate loops.\n", |
| 323 | + "</details>" |
271 | 324 | ]
|
272 | 325 | },
|
273 | 326 | {
|
274 | 327 | "cell_type": "markdown",
|
275 | 328 | "metadata": {},
|
276 | 329 | "source": [
|
| 330 | + "The code from exercise three in lesson one will probably be useful here! Compare against those random integers... \n", |
277 | 331 | "Like exercise three, if you want, you can add this to the cells at the end of this notebook (you may need to get your exercise 3 code if you do so): \n",
|
278 |
| - "<a href='#Exercise_cells'>[Click here to go there]</a> \n" |
| 332 | + "<a href='#Exercise_cells'>[Click here to go there]</a>\n" |
279 | 333 | ]
|
280 | 334 | },
|
281 | 335 | {
|
|
328 | 382 | "We will look at **AND**, **OR**, and **NOT**.\n",
|
329 | 383 | "\n",
|
330 | 384 | "<img src=\"Images/AND_table.PNG\" alt=\"A logical table for AND.\" /> \n",
|
| 385 | + "\n", |
331 | 386 | "**AND** returns True only if _both_ values being compared are true.\n",
|
332 | 387 | "\n",
|
333 | 388 | "<img src=\"Images/OR_table.PNG\" alt=\"A logical table for OR.\" /> \n",
|
| 389 | + "\n", |
334 | 390 | "**OR** returns True if _either_ value is true.\n",
|
335 | 391 | "\n",
|
336 | 392 | "**NOT** simply returns what it says! If you're checking if something is 'not False', you'll get a True value returned, if it isn't false. If it is False, your return will be False.\n",
|
|
425 | 481 | "`x % y` gives the remainder of x when divided by y. `4 % 2 = 0`, `4 % 3 = 1`, `7 % 3 = 1`. \n",
|
426 | 482 | "You need to check every number below your chosen number. You'll need a while loop.\n",
|
427 | 483 | "\n",
|
428 |
| - "If you want to save some time in your code: any non-prime number will have a factor less than or equal to its square root. We can use the `math` module for this: \n", |
| 484 | + "Efficiency extension if you want to save time running your code on large numbers:\n", |
| 485 | + "any non-prime number will have a factor less than or equal to its square root. We can use the `math` module for this: \n", |
429 | 486 | "math.sqrt() for square root.\n",
|
430 | 487 | "\n",
|
431 | 488 | "\n",
|
432 |
| - "# Exercise 8 -- Hangman\n", |
| 489 | + "# Extension: Exercise 8 -- Hangman\n", |
433 | 490 | "The game where you have to guess a word, one letter at a time. \n",
|
434 | 491 | "- How many guesses?\n",
|
435 | 492 | "- How do you display wrong guesses (you can only guess a letter once)?\n",
|
436 | 493 | "- Dealing with bad input -- how do make sure you can't enter numbers, capital letters (or no lowercase letters), punctuation\n",
|
437 | 494 | "\n",
|
438 |
| - "Tip, you can look through strings with this notation: \n", |
| 495 | + "Useful tips:\n", |
| 496 | + "\n", |
| 497 | + "- You can look through strings with this notation: \n", |
439 | 498 | "`string[0]`. This will return the first letter in the string. \n",
|
440 | 499 | "For example: `string = 'hello'` | `string[0] = 'h'` and `string[4] = 'o'` \n",
|
441 | 500 | "We'll cover this in more detail next lesson. \n",
|
442 | 501 | "\n",
|
443 |
| - "Using len(string), you can make a loop to check over every letter in a word!" |
| 502 | + "- Using len(string), you can make a loop to check over every letter in a word!\n", |
| 503 | + "\n", |
| 504 | + "- the word `in` can be used to check if something is in something else. (A character in a string, an item in a list, etc.). \n", |
| 505 | + "E.g.: `if \"a\" in \"apple\"` will be True." |
| 506 | + ] |
| 507 | + }, |
| 508 | + { |
| 509 | + "cell_type": "markdown", |
| 510 | + "metadata": {}, |
| 511 | + "source": [ |
| 512 | + "<details><summary>Ex.6: Step-by-Step</summary>\n", |
| 513 | + "\n", |
| 514 | + "1) Placing the 'making of the guess' into its own function is simply re-using the existing code from exercise 5 and calling a function instead. While this is not any more efficient, it demonstrates using functions for repeated code well, and is more readable.\n", |
| 515 | + "\n", |
| 516 | + "2) A 'guess quality' indicator will want the argument of the guess, and the number-to-guess. Decide how close is 'hot' and 'cold' and compare the two numbers to see how close they are, printing something to tell the user. You'll want to call this whenever the user makes a guess.\n", |
| 517 | + "\n", |
| 518 | + "3) If you have an idea for another function, consider what information the function will need to work (to pass as arguments) and what the correct time to call the function would be.\n", |
| 519 | + "</details>" |
| 520 | + ] |
| 521 | + }, |
| 522 | + { |
| 523 | + "cell_type": "markdown", |
| 524 | + "metadata": {}, |
| 525 | + "source": [ |
| 526 | + "<details><summary>Ex.7: Step-by-Step</summary>\n", |
| 527 | + "\n", |
| 528 | + "1) First, get a number from the user. Check if it's 1, because 1 is not prime!\n", |
| 529 | + "\n", |
| 530 | + "2) Create a loop, increasing a counter. Check if the current counter is a factor of the chosen number. If it is, the number isn't prime and you can break the loop.\n", |
| 531 | + "\n", |
| 532 | + "3) If you reach the end of the loop and don't find any factors, you can confidently say that it's not a prime number!\n", |
| 533 | + "\n", |
| 534 | + "</details>" |
| 535 | + ] |
| 536 | + }, |
| 537 | + { |
| 538 | + "cell_type": "markdown", |
| 539 | + "metadata": {}, |
| 540 | + "source": [ |
| 541 | + "<details><summary>Extension Ex.8: Step-by-Step</summary>\n", |
| 542 | + "\n", |
| 543 | + "There are a few ways you can make a game of hangman. With just the things we've covered so far, here's how I did it. After the course, I'd encourage you to give this one another go.\n", |
| 544 | + "\n", |
| 545 | + "1) First, you'll need to define a word to guess, the remaining turns, and set up a loop to count (up or down) the turns left.\n", |
| 546 | + "\n", |
| 547 | + "2) Then, take the user's guess. Make a few checks: is their guess the word? Ensure that the length (`len()`) of their guess is one character. Have they guessed the letter yet? You may want to store all of the `guesses` they've made using string concatenation and check using `in`.\n", |
| 548 | + "\n", |
| 549 | + "3) With all that setup, you need to display the word, displaying letters that the user has guessed (else, displaying `_` or some other blank). A loop, repeating over each letter of the `word`, using the string-searching notation mentioned above to check each letter, and comparing to the `guesses` like `in` step 2...\n", |
| 550 | + "\n", |
| 551 | + "4) If their guess completes the word, you'll need to let the user win. Counting how many letters are missing from the complete word, and winning if that is zero, would work nicely.\n", |
| 552 | + "\n", |
| 553 | + "5) If they lose, it would be nice to tell them what the word was!\n", |
| 554 | + "\n", |
| 555 | + "</details>" |
444 | 556 | ]
|
445 | 557 | },
|
446 | 558 | {
|
|
481 | 593 | }
|
482 | 594 | ],
|
483 | 595 | "metadata": {
|
| 596 | + "interpreter": { |
| 597 | + "hash": "278032a30cfb5c02e833568646a335a3fabfa17d7559ae8e8320799896ec3226" |
| 598 | + }, |
484 | 599 | "kernelspec": {
|
485 |
| - "display_name": "Python 3", |
| 600 | + "display_name": "Python 3.8.3 32-bit", |
486 | 601 | "language": "python",
|
487 | 602 | "name": "python3"
|
488 | 603 | },
|
|
496 | 611 | "name": "python",
|
497 | 612 | "nbconvert_exporter": "python",
|
498 | 613 | "pygments_lexer": "ipython3",
|
499 |
| - "version": "3.7.6" |
| 614 | + "version": "3.8.3" |
500 | 615 | }
|
501 | 616 | },
|
502 | 617 | "nbformat": 4,
|
|
0 commit comments