I am struggling with getting the below function call to execute the way I need it to in the below while loop.
def is_word_guessed(secret_word, letters_guessed):
secret_word_copy = list(secret_word[:])
for e in secret_word_copy:
if e not in letters_guessed:
return(False)
else:
all(e)
Once I have guessed all letters correctly this returns None.
Until all letters are guessed correctly this returns True.
If I replace the all(e) with return(False). It returns false as soon as one input(e) is found in secret_word_copy.
It is intended to return a usable return such as None or False when all ‘e’ in secret_word_copy appear in letters_guessed, so that I can exit the loop. A clear condition.
I may simply be using all(e) wrong but as return(False) is not working as I thought it should, it has been the only way I have found to get a change in the return when I need it.
I clearly understand this may not be the most pythonic code, the cleanest code, or overall even remotely how you would like to see this code implemented. I simply wanted to try to get the functional result of this code on my own before looking at other examples of and polishing this but I’ve hit a roadblock on something probably all together simple.
I also tried it like this
while is_word_guessed(secret_word, letters_guessed) == True:
just to see if the change from True to None would break the loop.
Anyway, any help pertaining to the mentioned function call or its use within the code as a whole would be greatly appreciated!
import string
secret_word = 'apple'
letters_guessed = []
def is_word_guessed(secret_word, letters_guessed):
secret_word_copy = list(secret_word[:])
for e in secret_word_copy:
if e not in letters_guessed:
return(True)
else:
all(e)
def get_guessed_word(secret_word, letters_guessed):
currect_guess = []
secret_word_copy = list(secret_word[:])
for e in secret_word_copy:
if e in letters_guessed:
currect_guess.append(e)
else:
currect_guess.append('_ ')
print(''.join(currect_guess))
def get_available_letters(letters_guessed):
for e in available_letters_copy:
if e in letters_guessed:
available_letters_copy.remove(e)
print(("Available Letters: "),' '.join(available_letters_copy))
def hangman(secret_word):
num_guesses = 6
print("Welcome to the game Hangman!")
print("I am thinking of a word that is", len(secret_word), "letters long.")
print("You have", num_guesses,"guesses left.")
while is_word_guessed(secret_word, letters_guessed):
while num_guesses != 0:
player_turn = input("Enter Guess: ")
letters_guessed.append(player_turn)
if player_turn not in secret_word:
num_guesses -= 1
print("Number of guesses remaining: ", num_guesses)
get_available_letters(letters_guessed)
get_guessed_word(secret_word, letters_guessed)
print("I'm sorry, that letter was not in the Secret Word.")
print(is_word_guessed(secret_word, letters_guessed))
else:
print(is_word_guessed(secret_word, letters_guessed))
get_available_letters(letters_guessed)
print("Number of guesses remaining: ", num_guesses)
get_guessed_word(secret_word, letters_guessed)
print("Good Guess!")
print("You have lost")
break
available_letters = string.ascii_lowercase
available_letters_copy = list(available_letters[:])
#secret_word = choose_word(wordlist)
hangman(secret_word)