Skip to content

A dual-mode 'Guess the Number' game in Python 3. Features both a standard user-guess mode and an algorithm-driven computer-guess mode that utilizes search space reduction logic to solve for the user's secret number.

Notifications You must be signed in to change notification settings

Digao075/guess_number

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ”ข Guess the Number (Dual Algorithms)

Python License

A robust Python implementation of the classic "Guess the Number" game featuring two distinct algorithms. This project demonstrates control flow, random number generation, and search space optimization logic.

๐Ÿš€ Key Features

  • Two Game Modes:
    1. User Guess: The player attempts to find a random number generated by the system.
    2. Computer Guess: The computer attempts to solve for the user's secret number using an adaptive algorithm.
  • Search Space Optimization: The computer_guess function dynamically adjusts its upper and lower bounds (high and low) based on feedback, mimicking a binary search strategy to narrow down the correct answer efficiently.
  • Edge Case Handling: Includes logic to prevent random.randint errors when the lower and upper bounds converge (low == high).

๐Ÿ› ๏ธ How It Works

Mode 1: User Guess

The program generates a random integer between 1 and x. The user receives feedback ("Too High" or "Too Low") until the correct number is identified.

Mode 2: Computer Guess (Algorithmic)

The computer guesses a number, and the user provides feedback:

  • H: The guess is too High (Computer adjusts high = guess - 1)
  • L: The guess is too Low (Computer adjusts low = guess + 1)
  • C: Correct

This feedback loop allows the computer to intelligently eliminate impossible numbers, converging on the solution much faster than random guessing.

๐Ÿ’ป Usage

  1. Clone the repository

    git clone [https://github.com/Digao075/guess-number-python.git](https://github.com/Digao075/guess-number-python.git)
    cd guess-number-python
  2. Run the script

    python main.py
  3. To change the range: Modify the function call at the bottom of the script:

    computer_guess(100) # Changes range to 1-100

๐Ÿง  Code Highlight: Algorithmic Logic

The following snippet demonstrates how the computer refines its search range. This logic prevents the computer from wasting guesses on numbers that have already been ruled out.

About

A dual-mode 'Guess the Number' game in Python 3. Features both a standard user-guess mode and an algorithm-driven computer-guess mode that utilizes search space reduction logic to solve for the user's secret number.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages