• We just launched and are currently in beta. Join us as we build and grow the community.

Python - Number Guessing Game

fgsdgf

Cybersecurity Architect
F Rep
0
0
0
Rep
0
F Vouches
0
0
0
Vouches
0
Posts
78
Likes
159
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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 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.
  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)

    :

  2. def

    __init__

    (

    self

    ,

    master)

    :
  3. Frame.__init__

    (

    self

    ,

    master)
  4. self

    .pack

    (

    )
  5. self

    .Create_Game

    (

    )
  6. self

    .number

    =

    random

    .randint

    (

    1

    ,

    101

    )

  7. def

    Create_Game(

    self

    )

    :
  8. self

    .Top

    =

    Frame(

    self

    ,

    bd=

    2

    ,

    relief=

    RIDGE)
  9. self

    .Top

    .pack

    (

    side=

    TOP,

    fill=

    X)
  10. Label(

    self

    .Top

    ,

    text=

    "Choose a number between 1 and 100."

    ,

    font=

    (

    'arial'

    ,

    14

    )

    )

    .pack

    (

    fill=

    X)


  11. self

    .guess_ent

    =

    Entry(

    self

    ,

    font=

    (

    'arial'

    ,

    18

    )

    )
  12. self

    .guess_ent

    .pack

    (

    pady=

    30

    )

  13. self

    .submit

    =

    Button(

    self

    ,

    text =

    "Submit"

    ,

    command=

    self

    .Start_Game

    )
  14. self

    .submit

    .pack

    (

    side=

    TOP,

    pady=

    10

    )

  15. self

    .text

    =

    Text(

    self

    ,

    bg=

    "black"

    ,

    fg=

    "white"

    ,

    wrap =

    WORD)
  16. self

    .text

    .pack

    (

    )

  17. def

    Restart_Game(

    self

    )

    :
  18. self

    .number

    =

    random

    .randint

    (

    1

    ,

    101

    )
  19. self

    .text

    .delete

    (

    0.0

    ,

    END)
  20. self

    .guess_ent

    .delete

    (

    0

    ,

    'end'

    )
  21. self

    .submit

    .config

    (

    text=

    "Submit"

    ,

    command=

    self

    .Start_Game

    )

  22. def

    Start_Game(

    self

    )

    :

  23. guess =

    int

    (

    self

    .guess_ent

    .get

    (

    )

    )

  24. if

    guess !=

    self

    .number

    :
  25. print_text =

    "You guessed {0}."

    .format

    (

    guess)

  26. if

    guess >

    self

    .number

    :
  27. print_text +=

    " That's too high. Guess lower..."
  28. elif

    guess <

    self

    .number

    :
  29. print_text +=

    " That's too low. Guess higher..."

  30. self

    .text

    .delete

    (

    0.0

    ,

    END)
  31. self

    .text

    .insert

    (

    0.0

    ,

    print_text)

  32. self

    .guess_ent

    .delete

    (

    0

    ,

    END)
  33. else

    :
  34. print_text =

    "You got it! Congrats!"
  35. self

    .text

    .delete

    (

    0.0

    ,

    END)
  36. self

    .text

    .insert

    (

    0.0

    ,

    print_text)
  37. 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.
  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!!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top