# Number guessing game in Python

The Number guessing game is all about guessing the number randomly chosen by the computer in the given number of chances.

Function to be used:

#### Explanation

In the program, while loop runs until the number guessed by the user is equal to the generated number or the number of attempts is less than the maximum number of chances. If the number of attempts becomes greater than the number of chances the game stops and the user loses the game. If the user guesses the correct number in the given number of chances then he or she will win. After every guess made by the user, the program informs the user whether the guessed number was smaller, greater than the actual generated number.
In this code, a random number is generated between 1-9 using the `randint()` function. The user is given a specific number of chances to guess the number if the chances exceed the user guesses the user would lose.

Below is the implementation.

 `# Number Guessing Game implementation   ` `# using Python  ` ` `  `  `  `import` `random ` ` `  `print``(``"Number guessing game"``) ` ` `  `# randint function to generate the  ` `# random number b/w 1 to 9 ` `number ``=` `random.randint(``1``, ``9``) ` ` `  `# number of chances to be given  ` `# to the user to guess the number  ` `# or it is the inputs given by user ` `# into input box here number of   ` `# chances are 5 ` `chances ``=` `0` ` `  `print``(``"Guess a number (between 1 and 9):"``)  ` ` `  `# While loop to count the number ` `# of chances ` `while` `chances < ``5``: ` `     `  `    ``# Enter a number between 1 to 9  ` `    ``guess ``=` `int``(``input``()) ` `     `  `    ``# Compare the user entered number   ` `    ``# with the number to be guessed  ` `    ``if` `guess ``=``=` `number: ` `         `  `        ``# if number entered by user   ` `        ``# is same as the generated   ` `        ``# number by randint function then   ` `        ``# break from loop using loop  ` `        ``# control statement "break" ` `        ``print``(``"Congratulation YOU WON!!!"``) ` `        ``break` `         `  `    ``# Check if the user entered   ` `    ``# number is smaller than   ` `    ``# the generated number  ` `    ``elif` `guess < number: ` `        ``print``(``"Your guess was too low: Guess a number higher than"``, guess) ` ` `  `    ``# The user entered number is   ` `    ``# greater than the generated  ` `    ``# number              ` `    ``else``: ` `        ``print``(``"Your guess was too high: Guess a number lower than"``, guess) ` `         `  `    ``# Increase the value of chance by 1 ` `    ``chances ``+``=` `1` `         `  `         `  `# Check whether the user   ` `# guessed the correct number  ` `if` `not` `chances < ``5``: ` `    ``print``(``"YOU LOSE!!! The number is"``, number) `

Inputs:

```1
5
7
9
8
```

Output:

```Number guessing game
Guess a number (between 0 and 10):
Your guess was too low: Guess a number higher than 1
Your guess was too low: Guess a number higher than 5
Your guess was too low: Guess a number higher than 7
Your guess was too high: Guess a number lower than 9
Congratulation YOU WON!!!
```

Note: In the above program, user can modify the value of randint() function to increase the range of numbers in this game and also user can increase the number of chances by increasing the value of the chance variable.

My Personal Notes arrow_drop_up 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.

Article Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.