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:
- random.randint(a, b): This method is used to generate random integer between a and b (both inclusive).
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.
1 5 7 9 8
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.
- Number Guessing Game in Python using Binary Search
- PyQt5 - Number Guessing Game
- Python program for word guessing game
- 21 Number game in Python
- Python implementation of automatic Tic Tac Toe game using random number
- Mastermind Game using Python
- Hangman Game in Python
- Hangman Game in Python
- Tic Tac Toe Game using PyQt5 in Python
- Python | Pokémon Training Game
- Python | Catching the ball game
- Color game using Tkinter in Python
- Conway's Game Of Life (Python Implementation)
- Python | Simple FLAMES game using Tkinter
- Python | Program to implement simple FLAMES game
- Introduction to pyglet library for game development in Python
- Python | Program to implement Jumbled word game
- Python | Program to implement Rock paper scissor game
- Variation in Nim Game
- 8-bit game using pygame
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.