Python - Number Guessing Game
Submitted by razormist on Thursday, August 2, 2018 - 07:06.
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.
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!!
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 https://www.python.org/downloads/.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.- from tkinter import *
- 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.- #=================================MAIN FRAME=========================================
- root = Tk()
- root.title("Number Guessing Game!")
- width = 400
- height = 320
- screen_width = root.winfo_screenwidth()
- screen_height = root.winfo_screenheight()
- x = (screen_width/2) - (width/2)
- y = (screen_height/2) - (height/2)
- root.geometry("%dx%d+%d+%d" % (width, height, x, y))
- 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.- class Application(Frame):
- def __init__(self, master):
- Frame.__init__(self, master)
- self.pack()
- self.Create_Game()
- self.number = random.randint(1, 101)
- def Create_Game(self):
- self.Top = Frame(self, bd=2, relief=RIDGE)
- self.Top.pack(side=TOP, fill=X)
- Label(self.Top, text="Choose a number between 1 and 100.", font=('arial', 14)).pack(fill=X)
- self.guess_ent = Entry(self, font=('arial', 18))
- self.guess_ent.pack(pady=30)
- self.submit=Button(self, text = "Submit", command=self.Start_Game)
- self.submit.pack(side=TOP, pady=10)
- self.text = Text(self, bg="black", fg="white", wrap = WORD)
- self.text.pack()
- def Restart_Game(self):
- self.number = random.randint(1, 101)
- self.text.delete(0.0, END)
- self.guess_ent.delete(0, 'end')
- self.submit.config(text="Submit", command=self.Start_Game)
- def Start_Game(self):
- guess = int(self.guess_ent.get())
- if guess != self.number:
- print_text = "You guessed {0}.".format(guess)
- if guess > self.number:
- print_text += " That's too high. Guess lower..."
- elif guess < self.number:
- print_text += " That's too low. Guess higher..."
- self.text.delete(0.0, END)
- self.text.insert(0.0, print_text)
- self.guess_ent.delete(0, END)
- else:
- print_text = "You got it! Congrats!"
- self.text.delete(0.0, END)
- self.text.insert(0.0, print_text)
- self.submit.config(text="Restart", command=self.Restart_Game)
Initializing the Application
After finishing the function save the application as index.py. 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.- #=================================INITIALIZATION======================================
- if __name__ == '__main__':
- app = Application(root)
- root.mainloop()
Add new comment
- 155 views