I have to write a code in Python where it is about playing the game hangman. Currently my code cannot replace the "_" with the guessed letters properly, where it only replaces one and doesn't show all of the guessed letters.
My code is below:
import random word = random.choice(["bread", "clouds", "plane"]) guess = 0 correct_guess = 0 play_again = True print(word) name = input("Hello, what's your name? ") print('Hello', name) status = input('Do you wish to play hangman? Yes/No ') hyphens = print('_ ' * (len(word))) if status == 'No' 'no' 'N' 'n': exit() while play_again is True: letter = input('Guess a letter: ') if letter in word: guess = guess + 1 correct_guess = correct_guess + 1 print('You guessed a letter correctly!') position = word.find(letter) print("The letter is in position", position + 1, "out of", len(word), "in the word. Guess Count:", guess) if position == 0: print(letter, "_ _ _ _") if position == 1: print("_", letter, "_ _ _ ") if position == 2: print("_ _", letter, "_ _ ") if position == 3: print("_ _ _", letter, "_ ") if position == 4: print("_ _ _ _", letter) else: guess = guess + 1 print("That letter isn't in the word. Guess Count:", guess) if guess == 10: print('Bad luck. You lost.') play = input('Do you want to play again? ') if play == 'No' 'no' 'N' 'n': exit() if correct_guess == 5: print('Congratulations! You have guessed the word! The word was', word + '.') play = input('Do you want to play again? ') if play == 'No' 'no' 'N' 'n': exit()
Your help is very much appreciated. Also, I don't know a lot about programming and I am fairly new to it.