Skip to content

Number Guessing Game Assignment

In this assignment, you'll build a simple number guessing game using Python. This exercise will help you practice user input handling, loops, conditionals, and error handling.

Structured Exercises:

  1. Generate a Random Number

    • Import the random module.

    • Generate a random number between 1 and 100.

  2. User Guess Input

    • Ask the user to guess the number.
    • Use an if/else statement to provide feedback:
      • If the guess is too high, print "Too high!".
      • If the guess is too low, print "Too low!".
      • If the guess is correct, print "Congratulations! You guessed it!" and exit the loop.
  3. Track Attempts

    • Use a while loop to keep the game running until the user guesses correctly.
    • Count the number of attempts and display the total attempts when the user wins.
  4. Handle Invalid Inputs (Task)

    • Modify the game to only accept valid integers.
    • If the user enters a non-integer value (e.g., letters or symbols), show an error message and ask for input again without crashing the program.

Bonus Challenge (Optional)

  • Set a limit (e.g., 5 attempts) and end the game if the user doesn't guess the number within the limit.
  • Allow the user to play again without restarting the script.

This assignment will strengthen your understanding of user input validation, loops, and conditionals. Good luck! 🚀