刽子手游戏中的提示

Hint in hangman game

我正在尝试像这样在输出中添加提示 >>

  1. --e - - ca
  2. p--i--a-

我怎么可以?

import random

word_list = ["india", "pakistan", "america"]
chosen_word = random.choice(word_list)

word_length = len(chosen_word)

display = []

for _ in range (word_length):
    letter = chosen_word[_]
    display += '_' 
print(display) 

lives = 8
game_over = False
while not game_over:
    guess = input("enter a guess:").lower()
    
    for position in range(word_length):
       
        random.choice(display[position])
        
        letter = chosen_word[position]
         
        if letter == guess:
            display[position] = letter
    if guess not in chosen_word:
        lives -= 1
    if lives == 0:

如果您总是希望您的提示包含 3 个字母并且字母应该是随机的,您可以这样做:

hint = ["-"]*word_length

# Gets 3 random positions to have for the letters
positions = random.sample(range(0,word_length), 3)

# At those positions, change the "-" in hint to a letter.
for i in positions:
  hint[i] = chosen_word[i]

hint = "".join(hint)
print(hint)