|
491 | 491 | "___\n", |
492 | 492 | "### <i class=\"fa-solid fa-pencil\"></i> Exercise 3: Fixing The number guessing game\n", |
493 | 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", |
| 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", |
496 | 529 | "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 | 530 | "\n", |
498 | 531 | "**Requirements**\n", |
|
547 | 580 | "- The `except Exception as e` block prints a helpful message and the program continues without crashing.\n", |
548 | 581 | "\n", |
549 | 582 | "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", |
550 | 624 | "````" |
551 | 625 | ] |
552 | 626 | }, |
|
0 commit comments