HANGMAN CODE- DAY 3
- MHK
- May 8, 2020
- 1 min read

DATE: MAY 8/ 2020
AUTHOR: MAHIR KAYA
Today, I will post a function called get_guessed_word which takes two parameters, secret_word and letter_guessed. This function is the second function in our Hangman assignment.
This function returns a word in format of a_ _ le. It inserts '_ ' for the unknown letters. It inserts '_ ' instead of '_' because user should be able to understand how many unknown letters are left in the word. If you insert '_' without space, user will see a__le instead of a_ _ le which makes the program look complicated.
If you see a mistake in our code or have suggestions about it, you can comment or tell us about it via members chat.
CODE:
def get_guessed_word (secret_word,letters_guessed):
secret_word=list(secret_word)#we turned secret_word into a list object.
secret_word_copy=secret_word[:]
# first we make a copy of secret_word to prevent problems while iterating.
for i in range(len(secret_word_copy)):
# We iterate over secret_word_copy
if secret_word_copy[i] not in letters_guessed:
secret_word.remove(secret_word[i])
# We remove the unknown elements from secret_word.
secret_word.insert(i,'_ ')
# We insert '_ ' in the place of unknown letters.
secret_word_2='' .join(secret_word)
# We turn the modified secret_word into a str and rename it as secret_word_2.
return(secret_word_2)
#returns secret_word_2.
#EXAMPLE YOU CAN TRY TO TEST:
#secret_word = 'apple' #letters_guessed = ['e', 'i', 'k', 'p', 'r', 's'] #print(get_guessed_word(secret_word, letters_guessed)) '_ pp_ e'
IMAGE ADRESS :
( however I wrote the 3 myself.)
Comments