Is there a way to insert a character into a string at a certain index? I am making a hangman game, and as players guess the letters, the "_" or blanks become filled with letters.
import random
import sys
forever = True
lives = 7
chosen_word = "none"
playing = True
length = "none"
letter = "none"
guessed = 0
user_input = False
words = [
"snag", "jungle", "important", "peasant", "baggage", "hail", "clog", "pizza", "sauce", "password", "scream",
"newsletter", "bookend", "pro", "dripping", "pharmacist", "lie", "catalog", "ringleader", "husband", "laser",
"diagonal", "comfy", "myth", "dorsal", "biscuit", "hydrogen", "macaroni", "rubber", "darkness", "yolk", "exercise",
"vegetarian", "shrew", "chestnut", "ditch", "wobble", "glitter", "neighborhood", "dizzy", "fireside", "retail",
"drawback", "logo", "fabric", "mirror", "barber", "jazz", "migrate", "drought", "commercial", "dashboard",
"bargain",
"double", "download", "professor", "landscape", "ski", "goggles", "vitamin",
]
print("Let's play Hangman! Guess the letters!")
chosen_word = (random.choice(words))
length = len(chosen_word)
while playing:
print("_" * length)
letter = (input(f"You have {lives} guesses left."))
if letter in chosen_word:
location = chosen_word.find(letter)
insert_word = list(chosen_word)
print(insert_word)
print("Great Job! You got a letter!")
else:
print("That letter is not in the word.")
lives -= 1
while forever:
if lives <= 0:
playing = False
print("You lost. Good game.")
user_input = (input("Would you like to play again?"))
if user_input == "False":
print("Good bye.")
sys.exit()
elif user_input == "True":
playing = True
print("Let's play Hangman again! Guess the letters!")
I have been able to locate the index where the character is supposed to go in, but I have no idea how to make the character replace the "_" already in that spot. I am currently trying to convert the word into a list first and insert the character in there.