SIZ Tutorial | HANGMAN GAME IN PYTHON In Less Than 70 Lines | 20% to @siz-official | by @sheham-sh
Hi everyone, it me, Sheham Hassan and I would like to share python code of Hangman Game which I've recently created in less than 70 lines of code.
This code is created by using basic concepts of Python like list, loops and conditions. You can even modify this code to add additional features.
Note: I've explained this code step by step and attached the entire code to the end of the article so you can easily copy it.
Step 1
The first step of hangman game is to import random module and decalre a list of word.
Here, I'vs declared the list named words, Use random.choice() function so that the different word will be stored in variable comp each time.
import random
j = 0
guess = ''
words = ["apple", "mango", "banana", "pineapple", "orange", "pear", "grape"]
comp = random.choice(words)
Step 2
Declare variables to draw hangman,I've declared 6 variable, which completes the hangman. Store all variable in a list named hang.
h1 = '''
O'''
h2 = '''
O
|'''
h3 = '''
O
\|'''
h4 = '''
O
\|/'''
h5 = '''
O
\|/
| '''
h6 = '''
O
\|/
| |'''
hang = [h1, h2, h3, h4, h5, h6]
Step 3
Create two empty list, complist and guessList. Then start a for loop which implement each character stored in comp, and store in list compList. For example: if comp = "apple", then compList = ["a", "p", "p", "l", "e"].
Similarly start another loop which store "-" character in guessList. For example: if comp = "apple", then guessList = ["-", "-", "-", "-", "-"].
compList = []
guessList = []
for i in comp:
compList.append(i)
for i in range(len(comp)):
guessList.append("-")
Step 4
Now create a function named display, declare a variable guessedWord in which every correct word is stored. This function displays guessList in single word.
def display():
guessedWord = ""
for k in guessList:
guessedWord += k
return guessedWord
Step 5
Now for the last step, which is basically the main body on a code. It contains an infinite while loop, which only ends if the player wins or loses. I've declared a variable guess which asks user to enter a character. If the user enters correct alphabet, it'll call display function. Or else it'll print each element of list hang after every wrong attempt.
print(f"\n{display()}")
while True:
if display() != comp:
guess = input("\nEnter a Guess: ")
if guess in compList and len(guess) == 1:
print("Correct")
r = compList.index(guess)
guessList[r] = guess
compList[r] = ""
print(display())
elif len(guess) > 1:
print("invalid choice")
else:
print("Incorrect")
print(hang[j])
j += 1
print(display())
if j == len(hang):
print("You Lost")
print(f"Correct Word: {comp}")
break
continue
print("Congratzzz..You Won")
break
Now just run the code and enjoy the game. The output will look like this
Here's the whole code from start to end:
import random
j = 0
guess = ''
words = ["apple", "mango", "banana", "pineapple", "orange", "pear", "grape"]
comp = random.choice(words)
h1 = '''
O'''
h2 = '''
O
|'''
h3 = '''
O
\|'''
h4 = '''
O
\|/'''
h5 = '''
O
\|/
| '''
h6 = '''
O
\|/
| |'''
hang = [h1, h2, h3, h4, h5, h6]
compList = []
guessList = []
for i in comp:
compList.append(i)
for i in range(len(comp)):
guessList.append("-")
def display():
guessedWord = ""
for k in guessList:
guessedWord += k
return guessedWord
print(f"\n{display()}")
while True:
if display() != comp:
guess = input("\nEnter a Guess: ")
if guess in compList and len(guess) == 1:
print("Correct")
r = compList.index(guess)
guessList[r] = guess
compList[r] = ""
print(display())
elif len(guess) > 1:
print("invalid choice")
else:
print("Incorrect")
print(hang[j])
j += 1
print(display())
if j == len(hang):
print("You Lost")
print(f"Correct Word: {comp}")
break
continue
print("Congratzzz..You Won")
break
That's it for my post, don't forget to support it.
Thanks
@cryptokraze | @arie.steem | @qasimwaqar | @vvarishayy | @suboohi
Congratulations you have been selected in best picks.
Finally...Thanksss @siz-official
Good one tutorial dear friend you make a very good post thanks for sharing a good information with us my best wishes for you.
Regards, Faran Nabeel
You can delegate your SP to @siz-official and take benefit of our Delegation Offer getting up to 200 % Curation rewards
501002003004005001000200030004000500010000