In the number guessing game, the user selects a number within a defined range and then the program guesses the number. If the number guessed is wrong then the user tells the program that is the actual number is greater than the guessed number or not. Similarly, the program again guesses the number until the actual number is not guessed.

**Approach:** The idea is to use binary search, where in each step the half portion of the search space is reduced. Below is the illustration of the approach:

- Intialize the start and end range of the number guessing.
- Guess the number as middle of the search space. That is
- If the number guessed is correct, then terminate the program.
- Otherwise, Ask the user that guessed number is less than the guessed number or not. If yes then reduce the search space accordingly.

Below is the implementation of the above approach:

## Python

`# Python implementation for the ` `# number guessing using ` `# Binary Search ` ` ` `# Global Arguments for playing game ` `args ` `=` `[` `"N"` `, ` `"N"` `, ` `"Y"` `] ` `index ` `=` `-` `1` ` ` `# Temporary function for taking ` `# input from the local arguments list ` `def` `input` `(): ` ` ` `global` `index, args; ` ` ` `index ` `+` `=` `1` ` ` `return` `args[index] ` ` ` `# Function to guess the number in ` `# a defined range of the number ` `def` `guessNumber(startRange, endRange): ` ` ` `if` `startRange > endRange: ` ` ` `return` `True` ` ` ` ` `# Middle of the range ` ` ` `mid ` `=` `(startRange ` `+` `endRange)` `/` `/` `2` ` ` ` ` `# Asking user about the ` ` ` `# actual number ` ` ` `print` `(` `"Is the number is "` `, ` ` ` `mid, ` `"?"` `, end ` `=` `" "` `) ` ` ` `user ` `=` `input` `() ` ` ` `print` `(user) ` ` ` ` ` `# Condition to check if the ` ` ` `# guessed number is actual number ` ` ` `if` `user ` `=` `=` `"Y"` `or` `user ` `=` `=` `"y"` `: ` ` ` `print` `(` `"Voila ! Successfully Guessed Number."` `) ` ` ` `return` `False` ` ` ` ` `# Condition to check if the ` ` ` `# guessed number is not correct ` ` ` `elif` `user ` `=` `=` `"N"` `or` `user ` `=` `=` `"n"` `: ` ` ` `print` `(` `"Actual number is greater than"` `,\ ` ` ` `mid, ` `"?"` `, end ` `=` `" "` `) ` ` ` `user ` `=` `input` `() ` ` ` `print` `(user) ` ` ` `if` `user ` `=` `=` `"Y"` `or` `user ` `=` `=` `"y"` `: ` ` ` `return` `guessNumber(mid` `+` `1` `, endRange) ` ` ` `elif` `user ` `=` `=` `"N"` `or` `user ` `=` `=` `"n"` `: ` ` ` `return` `guessNumber(startRange, mid` `-` `1` `) ` ` ` `else` `: ` ` ` `print` `(` `"Invalid Input. Print 'Y'/'N'"` `) ` ` ` `return` `guessNumber(startRange, endRange) ` ` ` ` ` `# Condition to check if the user ` ` ` `# input was invalid ` ` ` `else` `: ` ` ` `print` `(` `"Invalid Input. Print 'Y'/'N' "` `) ` ` ` `return` `guessNumber(startRange, endRange) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `print` `(` `"Number Guessing game in python"` `) ` ` ` `startRange ` `=` `1` ` ` `endRange ` `=` `10` ` ` `print` `(` `"Guess a number in range (1 to 10)"` `) ` ` ` ` ` `out ` `=` `guessNumber(startRange, endRange) ` ` ` ` ` `if` `out: ` ` ` `print` `(` `"Bad Choices"` `) ` |

*chevron_right*

*filter_none*

**Inputs:**

N N Y

**Outputs:**

Number Guessing game in python Guess a number in range (1 to 10) Is the number is 5 ? N Actual number is greater than 5 ? N Is the number is 2 ? Y Voila ! Successfully Guessed Number.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number guessing game in Python
- Number guessing game in Java
- PyQt5 - Number Guessing Game
- Guessing the Number Game using Android Studio
- Python program for word guessing game
- Total number of possible Binary Search Trees using Catalan Number
- Binary Search (bisect) in Python
- Floor value Kth root of a number using Recursive Binary Search
- Check if a given number is a Perfect square using Binary Search
- 21 Number game in Python
- Binary Search Visualization using Pygame in Python
- Meta Binary Search | One-Sided Binary Search
- Python Program for Binary Search (Recursive and Iterative)
- Find square root of number upto given precision using binary search
- Python implementation of automatic Tic Tac Toe game using random number
- Why is Binary Search preferred over Ternary Search?
- Game Theory (Normal-form Game) | Set 6 (Graphical Method [2 X N] Game)
- Game Theory (Normal form game) | Set 2 (Game with Pure Strategy)
- Game Theory (Normal-form Game) | Set 7 (Graphical Method [M X 2] Game)
- Game Theory (Normal-form game) | Set 3 (Game with Mixed Strategy)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.