How to Create a Guessing Game in Python

In this tutorial, we will program 'How to Create a Guessing Game in Python'. We will learn how to let the user guess a hidden word. The main goal here is to create a program that will display a hidden random word that the user will need to guess. I will provide a sample program to demonstrate the actual coding of this tutorial.

This topic is very easy to understand; just follow the instructions I provide, and you can also do it yourself with ease. The program I will show you covers the basics of programming for this guessing game. I will do my best to provide you with the easiest method for guessing the hidden word. So, let's start with 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/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. import random
  2.  
  3.  
  4. print("\n============== Guess a word Game ==============\n")
  5.  
  6. name = input("What is your name? ")
  7.  
  8.  
  9. print("Good Luck ! ", name)
  10.  
  11. words = ['rainbow', 'computer', 'science', 'programming',
  12. 'python', 'mathematics', 'player', 'condition',
  13. 'reverse', 'water', 'board', 'geeks']
  14.  
  15.  
  16. word = random.choice(words)
  17.  
  18.  
  19. print("Guess the word")
  20.  
  21. guesses = ''
  22.  
  23.  
  24. turns = 12
  25.  
  26.  
  27. while turns > 0:
  28.  
  29.  
  30. failed = 0
  31.  
  32.  
  33. for char in word:
  34.  
  35.  
  36. if char in guesses:
  37. print(char, end=" ")
  38.  
  39. else:
  40. print("_")
  41.  
  42. failed += 1
  43.  
  44. if failed == 0:
  45.  
  46. print("You Win")
  47.  
  48.  
  49. print("The word is: ", word)
  50. break
  51.  
  52.  
  53. print()
  54. guess = input("Enter a letter to guess: ")
  55.  
  56.  
  57. guesses += guess
  58.  
  59.  
  60. if guess not in word:
  61.  
  62. turns -= 1
  63.  
  64.  
  65. print("Wrong")
  66.  
  67.  
  68. print("You have", + turns, 'more guesses')
  69.  
  70. if turns == 0:
  71. print("You Loose")

This Python script implements a simple word guessing game. It randomly selects a word from a predefined list and prompts the player to guess the letters in the word. The player has a limited number of turns to guess the word. If the player correctly guesses all the letters, they win; otherwise, they lose after exhausting all their turns.

Output:

The How to Create a Guessing Game in Python source code that I provide can be download below. Please kindly click the download button.

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

More Tutorials for Python Language

Python Tutorials

Tags

Add new comment