'Hint in hangman game
Im trying to put hint in output like this >>
- --e - - ca
- 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:
Solution 1:[1]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AJH |
