我試圖在這樣的輸出中輸入提示>>
- --e - - ca
- 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:
uj5u.com熱心網友回復:
如果您總是希望您的提示包含 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/461230.html
標籤:Python python-3.x python-2.7 jupyter-笔记本
