Skip to content

Instantly share code, notes, and snippets.

@ganmahmud
Last active August 17, 2021 07:40
Show Gist options
  • Save ganmahmud/0ef567d7b2d526f2723b to your computer and use it in GitHub Desktop.
Save ganmahmud/0ef567d7b2d526f2723b to your computer and use it in GitHub Desktop.
Simple algorithm for the Hangman text-based game
import random
wList=['Earth','Fire','Wind','Water','Heart','By','your','power','combined','I','am','Captain','Planet']
ind=random.randint(0,10) # generating random integer.In exam the range was 0 to 100.But we will use 0 to 10 here
the_word_beta1=wList[ind]
the_word_beta2=the_word_beta1.lower() # making all the chars lower case for avoiding upper case-lower case match conflict
the_word=the_word_beta2.strip() # taking out all the white spaces before and after the word as there is one word per line in the words.txt. If we don't do it the length of the word will not be correct
print(the_word)
wLen=len(the_word)
u='*'*wLen
counter=0
# 12 attempts
while (counter!=12 and wLen!=0):
counter=counter+1
print (u)
guess_list=list(u)
g= input("Guess the letter: ")
for index,letter in enumerate(the_word):
if letter==g:
guess_list[index] = letter
wLen=wLen-1
u=''.join(guess_list)
if wLen==0:
print ("Congratulation!!!you have guessed the word")
print ("The word was",the_word)
else:
print ("sorry you loose")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment