Home > Mobile >  Hint in hangman game
Hint in hangman game

Time:04-18

Im trying to put hint in output like this >>

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

how can i?

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:

CodePudding user response:

If you always want your hint to contain 3 letters and the letters should be random, you could do:

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)
  • Related