In: Computer Science
Python3 Please add comments
Write a program that implements the word guessing game
- There is a secret word
- The user enters letters one at a time, if the letter appears in the secret word, it is revealed. Otherwise, it counts as a wrong guess.
If the user reveals all the letters in the word before getting too many wrong guesses then they win!
Otherwise, they lose.
1 - define secret word
2 - create a revealed letter list that starts empty
2. set wrong guess count to 0
3. ask the user for a letter
4. is the letter in the secret word?
5. if so: add it to the revealed letters list
create a blank string
for each letter in the secret word, if the letter in
revealed letter list, add that letter to string,
otherwise add an underscore to string
6. if letter not in secret word, add one to wrong guess
7. repeat from step 3
NOTE: change the secret word and guess limit as per your requirements.
PYTHON CODE:
# secret word
secret_word='python'
# list to store the revealed letter
revealed_letter=[]
# count for wrong guess
wrong_guess=0
# limit for guess
GUESS_LIMIT =6
# infinite loop
while True:
# getting a letter from the user
letter=input('Enter a letter: ')
# changing to lower case
letter=letter.lower()
# checking the letter in secret word
if letter in secret_word:
# if found, it is
added to the revealed_letter list
revealed_letter.append(letter)
# empty string variable
word=''
# for every letter in the secret word
for i in secret_word:
# if the letter in
revealed_letter list, then it is added
# to the string variable
'word'
if i in
revealed_letter:
word+=i
else:
# if not found, '_' is added to the variable
word+='_'
# if the user input letter not found in
secret word
# increment the wrong_guess count
if letter not in
secret_word:
wrong_guess+=1
# print the user guessed letter
print(word+'\n')
# deciding the win or lost
if word != secret_word and wrong_guess >=
GUESS_LIMIT:
print('You lost!')
break
elif word == secret_word and wrong_guess <
GUESS_LIMIT:
print('You win!')
break
else:
pass
SCREENSHOT FOR CODING:
SCREENSHOT FOR OUTPUT: