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).
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.
Recommended Posts:
- 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 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.

