Skip to main content

Python - Number Guessing Game

In this tutorial we will create a Number Guessing Game Using Python. Python has a design philosophy which emphasizes code readability. Python is very easy to learn the syntax emphasizes readability and it can reduces time consuming in developing. You can use this to convert the units value. So let's now do the coding.

Getting started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python

Importing Modules

After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python.

Then copy code that I provided below and paste it inside the IDLE text editor.

  1. from tkinter import *
  2. import random

Setting up the Main Frame

After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.

  1. #=================================MAIN FRAME=========================================
  2. root = Tk()
  3. root.title("Number Guessing Game!")
  4. width = 400
  5. height = 320
  6. screen_width = root.winfo_screenwidth()
  7. screen_height = root.winfo_screenheight()
  8. x = (screen_width/2) - (width/2)
  9. y = (screen_height/2) - (height/2)
  10. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  11. root.resizable(0, 0)

Creating the Main Function

This is where the code that contains the main funcitions. This code is consist of multiple functionalies, it includes all the widgets of the application as a OOP rendered. To do that just copy and write these blocks of code inside the text editor.

  1. class Application(Frame):
  3. def __init__(self, master):
  4. Frame.__init__(self, master)
  5. self.pack()
  6. self.Create_Game()
  7. self.number = random.randint(1, 101)
  9. def Create_Game(self):
  10. self.Top = Frame(self, bd=2, relief=RIDGE)
  11. self.Top.pack(side=TOP, fill=X)
  12. Label(self.Top, text="Choose a number between 1 and 100.", font=('arial', 14)).pack(fill=X)
  15. self.guess_ent = Entry(self, font=('arial', 18))
  16. self.guess_ent.pack(pady=30)
  18. self.submit=Button(self, text = "Submit", command=self.Start_Game)
  19. self.submit.pack(side=TOP, pady=10)
  21. self.text = Text(self, bg="black", fg="white", wrap = WORD)
  22. self.text.pack()
  24. def Restart_Game(self):
  25. self.number = random.randint(1, 101)
  26. self.text.delete(0.0, END)
  27. self.guess_ent.delete(0, 'end')
  28. self.submit.config(text="Submit", command=self.Start_Game)
  30. def Start_Game(self):
  32. guess = int(self.guess_ent.get())
  34. if guess != self.number:
  35. print_text = "You guessed {0}.".format(guess)
  37. if guess > self.number:
  38. print_text += " That's too high. Guess lower..."
  39. elif guess < self.number:
  40. print_text += " That's too low. Guess higher..."
  42. self.text.delete(0.0, END)
  43. self.text.insert(0.0, print_text)
  45. self.guess_ent.delete(0, END)
  46. else:
  47. print_text = "You got it! Congrats!"
  48. self.text.delete(0.0, END)
  49. self.text.insert(0.0, print_text)
  50. self.submit.config(text="Restart", command=self.Restart_Game)

Initializing the Application

After finishing the function save the application as This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.

  1. #=================================INITIALIZATION======================================
  2. if __name__ == '__main__':
  3. app = Application(root)
  4. root.mainloop()

There you have it we just created a Number Guessing Game Using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.