Number Guessing Game

Number Guessing Game using Tkinter

Number Guessing Game: We are just going to create an simple number guessing using python Tkinter. Creating number guessing system is very simpler and easy task using python. But as you know that Tkinter is an python’s GUI(Graphical User Interface). If you want to show this in user interactive way, you can do so using Tkinter. In this tutorial we will discuss about creating number guessing game in both ways, using simple python and using Tkinter.

What are random numbers?

It is simple and very basic level game i.e. just for fun. The idea behind that is using random numbers. Now the question arises that what is random number?  Till now you may listen number of definitions of random numbers. In simple words, random numbers are the numbers which are generated at dynamic time. And no one can predict the future value of random numbers. There are number of examples of random numbers around us. Like OTP(One Time Password) generation, Encryption etc.

To generate random numbers in python we can use its inbuilt module random. First of all you need to import this module using import keyword like below:

import random
     or
import random as something

About Number Guessing

  1. It is simple game, totally based on random numbers.
  2. In this tutorial we will discuss about creating this using simple python i.e. without GUI and using Tkinter GUI.
  3. Above both will use same methodology, except is just GUI.
  4. Here, first of all we will generate an random number using randint() of random module.
  5. After that, take input from user and compare this user input with the random number generated.
  6. If the match found, it will return message of success otherwise failure.

Number Guessing using simple python

import random
try:
    a = random.randint(10,20)
    i = 0
    while True:
        ui = int(input("*** Guess Any Number ***\n"))
        i = i+1
        if i==4:
            print("Maximum Guess Limit Reached!!!")
            break
        elif a > ui:
            print("Number You Guessed Is Lesser\n\n")
        elif a < ui:
            print("Number You Guessed Is Greater\n\n")
        elif a == ui:
            print("****Congratulations!!! You Cracked It!!!")
            print("Correct Match:{}\nYour Input: {}".format(a,ui))
            break
        else:
            print("Wrong Input")
except ValueError:
    print("Please Enter Integers Only!!!")

Output:

1. Working

Number Guessing Game
Number Guessing Game

2. If user try five or more times:

Number Guessing Game
Number Guessing Game

2. If user enters Non-integer value

Here we also done exception handling, except block will execute if it returns value error.

Number Guessing Game
Number Guessing Game

Number Guessing using python Tkinter(GUI)

It is very easy to convert simple python code to Tkinter. You just need to import Tkinter and place above code into tkinter syntax. Here is code for doing so:

from tkinter import *
import random
root = Tk()

def guess():
    try:
        a = random.randint(10,20)
        ui = int(ip.get())
        if a > ui:
            Lb2.config(text="Number You Guessed Is Lesser",bg='red')
        elif a < ui:
            Lb2.config(text="Number You Guessed Is Greater",bg='red')
        elif a == ui:
            Lb2.config(text="**Congratulations!!! You Cracked It!!!**\nCorrect Match:{}\nYour Input: {}".format(a,ui),bg='green')
        else:
            Lb2.config(text="Erong input!!! ",bg='red')
    except ValueError:
        Lb2.config(text="Please Enter integers only!!",bg='red')

Lb = Label(root, text="Guess Any Number",font=('cursive',20),bg='Yellow', fg='red')
Lb.pack(pady=5)
Lb2 = Label(root, fg='white')
Lb2.pack(pady=5)
ip = Entry()
ip.pack(pady=5)
btn = Button(text='Guess',width=15,bg='green',fg='white',command=guess)
btn.pack(pady=10)
btn2 = Button(text='Exit',width=15,bg='red',fg='white',command=exit)
btn2.pack(pady=5)
root.mainloop()

 Output

1. For wrong input if < correct match

Number Guessing Game
Number Guessing Game

2. For wrong input if > correct match

Number Guessing Game
Number Guessing Game

3. For right input if not integers

Number Guessing Game
Number Guessing Game

4. For right input if == correct match

Number Guessing Game
Number Guessing Game

You can Exit the game by click on Exit button.

Download source code

Download project on Github
Download project on Github 

So, it is all about number guessing. I hope you guys enjoyed the post.Thanks!!!

 

8 thoughts on “Number Guessing Game

Leave a Reply

Your email address will not be published. Required fields are marked *

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.