How to Write a Python Program for Guess the Number Game

In this tutorial we are writing Guess the Number Python Program.

First the program will print that it has a number in its mind, and the user have to guess it.User have infinite chances for guessing the right number.

Program Source Code : guess_the_number_Python_program.py

 1 import random
 2 mynum = random.randint(0, 9)
 3 
 4 print("I have a Number in my mind (0 - 9). can you guess it?")
 5 
 6 while True:
 7     usernum = int(input("Enter your Guess : "))
 8 
 9     if(mynum == usernum):
10         print("yes you are right")
11         break
12 
13     elif (mynum > usernum):
14         print("My number is greater than the number you entered. Try Again", end="\n\n")
15         
16     else:
17         print("My number is smaller than the number you entered Try Again", end="\n\n")

Program Output : Run 1

I have a Number in my mind (0 - 9). can you guess it?
Enter your Guess : 4
My number is greater than the number you entered. Try Again

Enter your Guess : 8
My number is smaller than the number you entered Try Again

Enter your Guess : 6
My number is smaller than the number you entered Try Again

Enter your Guess : 5
yes you are right

Video Tutorial : Python Program to Check Eligibility for Voting