Number Guessing Game with AI
Abstract
Challenge yourself against an AI in a number guessing game! The AI uses binary search to guess your number, then you try to guess the AI’s number. Compare rounds to see who wins and learn about efficient algorithms in action.
Prerequisites
- Python 3.6 or above
- Text Editor or IDE
- Basic understanding of Python syntax
- Understanding of loops and input/output
- Familiarity with binary search (helpful)
Getting Started
Create a new project
- Create a new project folder and name it
numberGuessingWithAI
numberGuessingWithAI
. - Create a new file and name it
numberguessingwithai.py
numberguessingwithai.py
. - Open the project folder in your favorite text editor or IDE.
- Copy the code below and paste it into your
numberguessingwithai.py
numberguessingwithai.py
file.
Write the code
- Add the following code to your
numberguessingwithai.py
numberguessingwithai.py
file.
⚙️ Number Guessing Game with AI
Number Guessing Game with AI
# Number Guessing Game with AI
# Credits: https://gist.github.com/ashishanand7/61bb95a556e1575a63b1745c5fd48a4a
import random
print("NUMBER GUESSING GAME . YOU vs AI \n AI's turn is first\n")
print('Think of a number a number between 0 and 100. ')
input('Press Enter when done')
m=50
j=100
i=0
c=0
print('\nAnswer with:\n "h" if your number is bigger.\n "l" if your number is smaller.\n "y" if AI guesses the correct number')
while True:
x=input('is it '+str(m)+' ?\n')
c+=1
if x=="y":
print("BINGO")
break
elif x=="h":
i=m
m=round(((m+j)/2),0)
elif x=="l":
j=m
m=round(((m+i)/2),0)
else:
print("Invalid input")
c-=1
print("Now it's your turn to guess which number computer has thought of between 0 and 100")
a=random.randint(0,101)
d=0
while True:
x=int(input('Tell which number AI has picked between 0 & 100\n'))
d+=1
if x==a:
print('BINGO . You guessed right.')
break
elif x<a:
print('you guessed too low .')
elif x>a:
print('you guessed too high .')
if c<d:
print("The AI guessed it in ",c," rounds while you took ",d," rounds .\n AI WINS :D\n Robots are your Overlords . Accept it !!!")
elif c==d:
print("Both AI & you took ",c," rounds . So , it's a TIE :|")
else:
print("The AI guessed it in ",c," rounds while you took ",d," rounds .\nYOU WON :\ \nSavour the few wins left before robots outsmart you forever ...")
Number Guessing Game with AI
# Number Guessing Game with AI
# Credits: https://gist.github.com/ashishanand7/61bb95a556e1575a63b1745c5fd48a4a
import random
print("NUMBER GUESSING GAME . YOU vs AI \n AI's turn is first\n")
print('Think of a number a number between 0 and 100. ')
input('Press Enter when done')
m=50
j=100
i=0
c=0
print('\nAnswer with:\n "h" if your number is bigger.\n "l" if your number is smaller.\n "y" if AI guesses the correct number')
while True:
x=input('is it '+str(m)+' ?\n')
c+=1
if x=="y":
print("BINGO")
break
elif x=="h":
i=m
m=round(((m+j)/2),0)
elif x=="l":
j=m
m=round(((m+i)/2),0)
else:
print("Invalid input")
c-=1
print("Now it's your turn to guess which number computer has thought of between 0 and 100")
a=random.randint(0,101)
d=0
while True:
x=int(input('Tell which number AI has picked between 0 & 100\n'))
d+=1
if x==a:
print('BINGO . You guessed right.')
break
elif x<a:
print('you guessed too low .')
elif x>a:
print('you guessed too high .')
if c<d:
print("The AI guessed it in ",c," rounds while you took ",d," rounds .\n AI WINS :D\n Robots are your Overlords . Accept it !!!")
elif c==d:
print("Both AI & you took ",c," rounds . So , it's a TIE :|")
else:
print("The AI guessed it in ",c," rounds while you took ",d," rounds .\nYOU WON :\ \nSavour the few wins left before robots outsmart you forever ...")
- Save the file.
- Run the following command to run the application.
command
C:\Users\username\Documents\numberGuessingWithAI> python numberguessingwithai.py
Think of a number between 0 and 100!
Is your number 50? (h/l/y): h
Is your number 75? (h/l/y): l
Is your number 62? (h/l/y): l
Is your number 56? (h/l/y): y
I guessed your number in 4 rounds!
Now guess my number between 0 and 100:
50
Too low! Try again: 75
Too high! Try again: 62
Correct! You guessed my number in 3 rounds!
You win this round!
command
C:\Users\username\Documents\numberGuessingWithAI> python numberguessingwithai.py
Think of a number between 0 and 100!
Is your number 50? (h/l/y): h
Is your number 75? (h/l/y): l
Is your number 62? (h/l/y): l
Is your number 56? (h/l/y): y
I guessed your number in 4 rounds!
Now guess my number between 0 and 100:
50
Too low! Try again: 75
Too high! Try again: 62
Correct! You guessed my number in 3 rounds!
You win this round!
Explanation
- The
import random
import random
statement imports the random module for generating random numbers. - The AI uses binary search algorithm to efficiently guess your number.
- The
low
low
andhigh
high
variables maintain the search range for the AI. - The AI starts by guessing the middle value (50) and adjusts based on your response.
- Your responses (‘h’ for higher, ‘l’ for lower, ‘y’ for yes) guide the AI’s search.
- The
random.randint(0, 100)
random.randint(0, 100)
generates a random number for you to guess. - The program tracks rounds taken by both AI and human player.
- Winner determination compares the number of rounds each player needed.
- The binary search algorithm guarantees the AI will find any number in at most 7 rounds.
- The game demonstrates the efficiency of algorithmic problem-solving vs. human intuition.
Next Steps
Congratulations! You have successfully created a Number Guessing Game with AI in Python. Experiment with the code and see if you can modify the application. Here are a few suggestions:
- Add difficulty levels (change range from 0-100 to larger ranges)
- Track high scores and statistics across multiple games
- Add GUI with Tkinter for better user experience
- Let AI learn from previous games to improve performance
- Implement different AI strategies (random, pattern-based)
- Add multiplayer support for multiple human players
- Create tournament mode with best-of-series gameplay
- Add sound effects and visual feedback
- Implement hints system for human player
Conclusion
In this project, you learned how to create a Number Guessing Game with AI in Python. You also learned about binary search algorithms, human vs. computer problem-solving, and competitive programming concepts. You can find the source code on GitHub
Was this page helpful?
Let us know how we did