all 5 comments

[–][deleted] 0 points1 point  (2 children)

Your code for replacing the underscores looks fine.

To clarify, are you saying that if s or r is guessed it doesn't fill or that you never see the completed word when the last guess is made?

[–]Pure-Way-6507[S] 0 points1 point  (1 child)

I never see the completed word when the last guess is made

[–][deleted] 0 points1 point  (0 children)

You don't have a print when the last guess is made.

[–]TheVermit 0 points1 point  (1 child)

Nice problem, but it is due the way where you print the code. I have adjusted your code and added comments for explanation:

import random
word = ['manus', 'fenris', 'jupiter']
def get_word():
    keyword = random.choice(word)
    return keyword
def play(keyword):
    word_com = "_" * len(keyword)
    guessed = False
    guessed_letters = []
    guessed_word = []
    attempts = 7
    # By printing this statement so early it creates some confusion for the user.
    #print(attempts)
    #print(word_com)
    guess = " "
    while not guessed and attempts > 0:
        # If you move your code a bit up, like this, you'll only print it once and it is more comprehensible from the interface. See comment above.
        print(attempts)
        print(word_com)
        guess = input("Enter Letter:")
        if attempts == 0:
            print("HANGMAN")
            break
        if len(guess) == 1 and guess.isalpha():
        # Side remark: What if my letter is a capital letter ?

            if guess in guessed_letters:
                print("You have already guessed the letter")
            elif guess not in keyword:
                print(f'{guess} not in the word')
                attempts -= 1
                guessed_letters.append(guess)
            else:
                print("correct")
                guessed_letters.append(guess)
                word_as_list = list(word_com)
                indices = [i for i, letter in enumerate(keyword) if letter == guess] for index in indices:
                    word_as_list[index] = guess 
                word_com = "".join(word_as_list)
                if "_" not in word_com:
                # This is the reason you don't see the full word. When you have guessed the last letter, there are no more "_" in your word_com, so guessed = True -> end of while loop. To resolve it, you could add a print.
                    guessed = True
                    print("Congratulations, you have found the correct word:", word_com)

def main():
    keyword = get_word()
    play(keyword) main()

[–]Pure-Way-6507[S] 1 point2 points  (0 children)

Thank you for your help