top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Guess Number Game in Python

Writer's picture: Rashidat SikiruRashidat Sikiru


In this article, we are going to make a “Guess the Number” game.


How it works

Our games will randomly generate a number between 0 and 30 and the player has to guess the number. If the number entered by the player is less than generated number then the player will be prompted with too low message! And if the number entered by the player is more than generated number then the player will be prompted with too high message! This process will be repeated until the player finds the right number.


Let's Code

Now since we are going to generate a random number and if you've been following past tutorials in the series than you might know what we are talking about here. We are going to import a module that comes pre-installed with our Python, it's called random.


Step 1 : Let's import it into our project.

import random

Step 2 : Now we have to initialize a max_num variable. So that we can customize the difficulty as per our choice. The higher the value of max_num, the higher the difficulty. For now, let's keep it 30

max_num = 30

Step 3 : Now it's time we generate our random number which the player has to find. For that we will be using randint() function from the random module. We will store this random value in random_number variable.

random_number = random.randint(1, max_num)

It will generate a random number between 1 & max_num.


Step 4 : Then we will also initialize a guess variable to store the player's answer for comparison.

guess = 0

Step 5 : Now let's create a while loop to keep asking the player until the right number is found. We will keep this loop running until our random_number & guess matches.

while guess != random_number:
    pass

Step 6 : so it's time we ask our player to make a guess enter a number to start the game.

while guess != random_number:
    guess = int(input(f"Guess the number between 1 &{max_num}: "))

We will also use int() function to covert the number from a string into an integer so that we can compare it to check the answer.


Step 6 : Now we will make use of if conditionals to compare the answer and provide suitable feedback to the player.

while guess != random_number:
    guess = int(input(f"Guess the number between 1 &{max_num}: "))
    if guess < random_number:
        print("Wrong! Too low...")
    elif guess > random_number:gfd
        print("Wrong! Too high...") 
    else:
        print(f"Thats Right! Random number is {random_number}")
 

Happy coding !!!

0 comments

Recent Posts

See All

Comments


COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page