Hey python geeks, are you interested in game development. So this Python Number Guessing Game tutorial will help you to implement number guessing game in python. That’s not so hard, extremely easy, you have to just basics knowledge of python. So let’s start to implement it.

Number Guessing game is very interesting, i like it so much. So first of all we need to understand the basic logic behind this game. So let’s start Python Number Guessing Game tutorial. I hope you will surely enjoy this.

## Python Number Guessing Game Tutorial – Play With Your Computer

Before implementing it in code, we have to understand the logic of this game. So now i am explaining a deep description about **Number Guessing Game.**

### What Is Number Guessing Game

I am explaining it in context of computer and a person.

- In this game two players are required.
- The first player(computer) thinks about a random number within a given range and another player(person/user) have to guess that number.
- If the guessed number not matched with the random number then computer tells the user – the guessed number is too low or too high.
- User will keep guessing numbers until user find the computer’s number, and the computer will tell user each time if user’s guess was too high or too low:
- Eventually, the user guesses the correct number, and this way game will quit.

#### Key Strategy

The key strategy in this game is to generate a clever guess.

**For example**

- If user knows the number is in between 0 and 50, then he/she make a reasonable guess i.e., 25.
- This choice evenly splits the range, that will help you making a next guess.
- If the computer says the guess is too low, then the user splits the reduced range and guesses 12. If the player says the guess is too high, then the optimal guess is 6. It can be shown that by splitting the remaining range in half after each guess.

### Getting Started Python Number Guessing Game

Now let’s start implementing code for this.

So the game completes by following 4 steps

- Computer pick a random number
- Player makes a guess
- Compare guess to the number
- Print out
**“Too High” , “Too Low”**or**” You got it”.**

#### Computer Pick A Random Number

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import random random_number = random.randint(1,10) tries = 1 username = input("Hello, What's your Username") print("Hello", username+",", ) question = input("Would you Like To Play A Game ? [Y/N] ") if question == "n": print("oh..okay") if question == "y": print("I'm Thinking Of A Number Between 1 & 10") |

##### Explanation

- First of all import random module. random module implements pseudo-random number generators for various distributions.
- Then initialize a variable that stores random number and call
**randint()**method. **randint()**function return a random integer*N*such that`a <= N <= b`

. In parameter we have to pass the range of random number. Here i am taking range between 1-10.- Define a variable tries and initialize with 1.
- Then ask user to input their username, and print username along with greeting.
- And now ask user to input their choice whether they want to play game or not.
- And now check If the user’s answer is no then print
**oh..okay.** - If the user’s answer is yes then print
**I’m Thinking Of A Number Between 1 & 10.**

#### Player Makes A Guess

1 2 3 |
guess = int(input("Have a Guess :")) |

- In this user makes a guess.
- Define a variable and ask user to enter their guess.

#### Compare Guess To The Number

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
if guess > random_number: print("Guess Lower...") if guess < random_number: print("Guess Higher...") while guess != random_number: tries += 1 guess = int(input("Try Again : ")) if guess <random_number: print("Guess Higher...") if guess == random_number: print("You're Right! you win! The Number Was", random_number, "and it only", tries, "tries!") |

- Now check if the user’s guess is greater than random number then print guess lower and if the user’s guess is less than random number then print guess higher.
- And now start a while loop, this will repeat until
**guess != randum_number .** - Increase tries by 1, each time loop repeat.
- Again ask the user to try again.
- And finally if guess becomes equal to random number then print the winning results.

### Complete Code For Python Number Guessing Game

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import random random_number = random.randint(1,10) tries = 1 username = input("Hello, What's your Username") print("Hello", username+",", ) question = input("Would you Like To Play A Game ? [Y/N] ") if question == "n": print("oh..okay") if question == "y": print("I'm Thinking Of A Number Between 1 & 10") guess = int(input("Have a Guess :")) if guess > random_number: print("Guess Lower...") if guess < random_number: print("Guess Higher...") while guess != random_number: tries += 1 guess = int(input("Try Again : ")) if guess <random_number: print("Guess Higher...") if guess == random_number: print("You're Right! you win! The Number Was", random_number, "and it only", tries, "tries!") |

#### Let’s Start Play

Now we will start playing this game, so let’s see what’s the result.

This way you can implement number guessing game in python and play it.

## Recommended Articles :

- Python Calculator – Create A Simple GUI Calculator Using Tkinter
- Python Chatbot – Build Your Own Chatbot With Python
- Python Sending Email Tutorial using SMTP Protocol
- Python Rest API Example using Bottle Framework

So this was Python Number Guessing Game tutorial. If you have any doubt then leave your comment. And if you found it helpful then please share with others. Thanks