How do you make a guess game in python?

This script is an interactive guessing game, which will ask the user to guess a number between 1 and 99.

We are using the random module with the randint function to get a random number. The script also contains a while loop, which make the script run until the user guess the right number.

If you read my previous post about conditional statements in Python, you will also recognize the if, elif and else statements.

How do you make a guess game in python?

import random
n = random.randint(1, 99)
guess = int(raw_input("Enter an integer from 1 to 99: "))
while n != "guess":
    print
    if guess < n:
        print "guess is low"
        guess = int(raw_input("Enter an integer from 1 to 99: "))
    elif guess > n:
        print "guess is high"
        guess = int(raw_input("Enter an integer from 1 to 99: "))
    else:
        print "you guessed it!"
        break
    print

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Today we are going to make an interactive guessing game in Python.

This is going to be a simple guessing game where the computer will generate a random number between 1 to 10, and the user has to guess it in 5 attempts.

Based on the user's guess computer will give various hints if the number is high or low. When the user guess matches the number computer will print the answer along with the number of attempts.

This is how the game looks in action,

Hello, What's your name?

Abhijeet

okay! Abhijeet I am Guessing a number between 1 and 10:

2
Your guess is too low
4
Your guess is too low
6
You guessed the number in 3 tries!

In this article, we will guide you through each step of making this interactive guessing game in Python.

Now, open your favorite text editor and start coding.

First, we will create a file a new file named game.py from our text editor.

To generate a random number we will use a Python module named random to use this module in our program, we first need to import it.

 import random

Next, we will use the random module to generate a number between 1 to 10 and store it in a variable named number.

number = random.randint(1, 10)

Now we will prompt the user to enter his name and store it to a variable named player_name.

player_name = input("Hello, What's your name?")

In the next step, we will create a variable named number_of_guesses and assign 0 to it. Later we will increase this value on each iteration of the while loop.

Finally, before constructing the while loop, we will print a string which includes the player name.

 print('okay! '+ player_name+ ' I am Guessing a number between 1 and 10:')

Now let's design the while loop.

while number_of_guesses < 5:
    guess = int(input())
    number_of_guesses += 1
    if guess < number:
        print('Your guess is too low')
    if guess > number:
        print('Your guess is too high')
    if guess == number:
        break

In the first line, we are defining the controlling expression of the while loop. Our game will give user 5 attempts to guess the number, hence less than 5 because we have already assigned the number_of_guesses variable to 0.

Within the loop, we are taking the input from the user and storing it in the guess variable. However, the user input we are getting from the user is a string object and to perform mathematical operations on it we first need to convert it to an integer which can be done by the Python's inbuilt int() method.

In the next line, we are incrementing the value of number_of_guesses variable by 1.

Below it, we have 3 conditional statements.

  1. In the first, if statement we are comparing if the guess is less than the generated number if this statement evaluates to true, we print the corresponding Guess.
  2. Similarly, we are checking if the guess is greater than the generated number.
  3. The final if statement has the break keyword, which will terminate the loop entirely, So when the guess is equal to the generated number loop gets terminated.

Below the while loop, we need to add another pair of condition statements,

if guess == number:
    print('You guessed the number in ' + str(number_of_guesses) + ' tries!')
else:
    print('You did not guess the number, The number was ' + str(number))

Here we are first verifying if the user has guessed the number or not. if they did, then we will print a message for them along with the number of tries.

If the player couldn't guess the number at the end we will print the number along with a message.

If you have been following us, then this is how your program should look like:

import random
number = random.randint(1, 10)

player_name = input("Hello, What's your name?")
number_of_guesses = 0
print('okay! '+ player_name+ ' I am Guessing a number between 1 and 10:')

while number_of_guesses < 5:
    guess = int(input())
    number_of_guesses += 1
    if guess < number:
        print('Your guess is too low')
    if guess > number:
        print('Your guess is too high')
    if guess == number:
        break
if guess == number:
    print('You guessed the number in ' + str(number_of_guesses) + ' tries!')
else:
    print('You did not guess the number, The number was ' + str(number))

Now let's run our game!

To run the game, type this in your terminal python game.py and hit Enter.

This was it, if you got stuck somewhere grab the code form Github repo

PYTHON

How do you make a game using Python?

Here is the following example of creating a simple pygame window..
import pygame..
pygame.init().
screen = pygame.display.set_mode((400,500)).
done = False..
while not done:.
for event in pygame.event.get():.
if event.type == pygame.QUIT:.
done = True..

How do you guess the number game in Python 3?

Python Code: randint(1, 10), 0 while target_num != guess_num: guess_num = int(input('Guess a number between 1 and 10 until you get it right : ')) print('Well guessed!') Sample Output: Guess a number between 1 and 10 until you get it right : 5 Well guessed!

How do you randomly guess a word in Python?

choice() function will choose one random word from the gven list of words. word1 = rdm. choice(words1) print ("Please guess the characters: ")

What is number guessing game in Python?

In this game, the program generates random number but this number is not visible to the player. Player tries to guess the number. If the player enters the same number that is generated by system then program displays the winning message and game ends there.