Number Guessing Game in Python using Binary Search Last Updated : 27 Jan, 2023 Comments Improve Suggest changes Like Article Like Report 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: Initialize the start and end range of the number guessing.Guess the number as middle of the search space. That is Number = \frac{startRange + endRange}{2} 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") 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. The time complexity of this algorithm is O(log n) as we divide the range in half with each iteration of the guessNumber function. The space complexity of this algorithm is O(1) as we only use a constant number of variables that do not increase with the size of the input. Comment More infoAdvertise with us Next Article Number Guessing Game in Python using Binary Search M mynameisshrish Follow Improve Article Tags : Python Practice Tags : python Similar Reads Number Guessing Game Using Python Streamlit Library The game will pick a number randomly from 1 to 100 and the player needs to guess the exact number guessed by calculating the hint. The player will get 7 chances to guess the number and for every wrong guess game will tell you if the number is more or less or you can take a random hint to guess the n 4 min read Number Guessing Game Using Python Tkinter Module Number Guessing Game using the Python Tkinter module is a simple game that involves guessing a randomly generated number. The game is developed using the Tkinter module, which provides a graphical user interface for the game. The game has a start button that starts the game and a text entry field wh 5 min read Number guessing game in Python 3 and C The objective of this project is to build a simple number guessing game that challenges the user to identify a randomly selected number within a specified range. The game begins by allowing the user to define a range by entering a lower and an upper bound (for example, from A to B). Once the range i 4 min read Binary Search Visualization using Pygame in Python An algorithm like Binary Search can be understood easily by visualizing. In this article, a program that visualizes the Binary Search Algorithm has been implemented. The Graphical User Interface(GUI) is implemented in Python using pygame library. Approach Generate random array, sort it using any sor 4 min read Ceil in a Binary Search Tree Python In a Binary Search Tree (BST), the ceiling of a given value is the smallest element in the tree that is greater than or equal to the given value. Finding the ceiling in a BST can be a useful operation when working with ordered data. In this article, we will explore how to find the ceiling in a Binar 3 min read Like