I got a ton of helpful tips last time I posted some code, so I thought I would come back to the well.
My function is deciding whether or not the secretWord
has been guessed in my hangman game. I am trying to accomplish this by taking a list, lettersGuessed
, and comparing it to the char in each index of the string secretWord
. My code works, but I feel as though it is not optimal nor formatted well.
def isWordGuessed(secretWord, lettersGuessed):
'''
secretWord: string, the word the user is guessing
lettersGuessed: list, what letters have been guessed so far
returns: boolean, True if all the letters of secretWord are in lettersGuessed;
False otherwise
'''
chars = len(secretWord)
place = 0
while place < chars:
if secretWord[0] in lettersGuessed:
place += 1
return isWordGuessed(secretWord[1:], lettersGuessed)
else:
return False
return True
I tried to use a for loop (for i in secretWord:
) originally, but I could not get my code to return both True and False. It would only do one or the other, which is how I ended up with the while loop. It seems that while
loops are discouraged/looked at as not very useful. Is this correct?
Also, I am wondering if the recursive call is a good way of accomplishing the task.