I have been trying to solve this problem because it is a part of constructing Hangman game.
My problem is that I need to write a code which would check if all the list items are in the secret word. If all are in a list, it will return True, if not, False.
This is my work I have done:
def isWordGuessed(secretWord, lettersGuessed):
a_list = []
for i in range(0, len(lettersGuessed)):
a_list.append(lettersGuessed[i])
str1 = ''.join(a_list)
if str1 in secretWord:
return 1
else:
return 0
isWordGuessed('apple', ['e', 'i', 'k', 'p', 'r', 's'])
However, I have tried to do this:
isWordGuessed(‘apple’, [‘a’, ‘p’, ‘l’, ‘p’, ‘e’])
It returns False, even though all letters in a list are in ‘apple’
I guess I could solve this without making a string but I don’t how. By the way, this must work for any word and list given. If all list items are in a word, it is true, otherwise false…Any help?
I would convert the secret word and the already guessed letters both to sets and check whether the set of the characters of the secret word is a subset of the set of the guessed characters: