Detailed explanation of hangman using python and snippets
- About-Hangman
- creating-xlsx-file
- input-for-adding-words
- to-and-fro-in-xlsx
- choosing-random-word
- trial-and-errors
- unveiling-word
- result-prediction
- summary
Hangman is a popular word prediction game in which the player tries to piece together a missing word
one letter at a time.
The game ends after a specific amount of incorrect assumptions are made, and the player loses.
Meanwhile when the player correctly identifies all of the letters of the lost word, the game is likewise over.
Here I used xlsxwriter module to create a xlsx file, which takes input from the user to create a file. xlrd module is used to read the words from the file so user have multiple options for inputs. commands to install the module are below
pip install xlsxwriter
pip install xlrd
Now comes the tricky part if you're a beginner and struggling with syntatical errors like giving wrong parameters etc. Below line is the only script used to take input from user.
b = list(map(str, input("Enter the words in a single go: ").strip().split()))[:n]
let's divide it into 5 part's to avoid confusion
list():
The list function is used to convert other datatype to list which was clearly stated by me in one of my REPO
map():
We can pass multiple iterable arguments to the map () function. In that case, the specified function must have that many arguments. The function will be applied to these iterable elements in parallel. With multiple iterable arguments, the map iterator stops when the shortest iterable is exhausted.
strip():
The strip function removes whitespace's that are present at the beginning and at the ending of the string.
split():
This divides a given sentence into words the default parameter for it is whitespace but anything can be given otherthan that.
[:n]
The slice operator reduces the amount of given value into certain index value. ":" colon is also known as slice operator in python. Here the n is no.of inserting elements and the slice operator is given n as end value as it's after the operator. the slice operater make sure to remove exceeding amount of words than given with the help of indexing.
Here for chooisng random word we used python random module which is a inbuilt so installation is required.
import random
a = ['Gas', 'Food', 'Gym', 'Health']
word = random.choice(a)
print(word)
choice command in random modules chooses any one of the given data and return it to the respective variable.
In the module 'xlsxwriter' we have a function called write() which takes parameter's as row, col ,item Here item refers to the element that needs to be inserted into the sheet Loop it up and you are all good to go
worksheet = workbook.add_worksheet()
worksheet.write(row, col, item)
These are nothing but bunch of if and else conditions shown below.
guess = input("\nguess a character:")
your_guess += guess
if guess not in word:
turns -= 1
print("\nWrong")
print("\nYou have", + turns, 'more guesses')
if turns == 0:
print("\nYou Lose")
Firstly it takes input from the user and adds it the total guesses.
if the current guess is not present in the random word then one point is reduced and warning statement's are shown.
The other way it ignores as my crush did and continues to loop unitll you have 0 turns left or you've won the game.
for char in word:
if char in your_guess:
print(char, end="")
else:
print("_", end="")
The above loop will be insides another while loop, so in consideration for it the for loop decides to show the true characters you've entered. if block shows the char if you've entered it previously or the else block comes into action and provides empty blank space for it.
Well leaving the whole load of features aside if you came here only for the hangman you can visit this doc to get the simpler version without traction