如何替换隐藏的字母挂满

2024-04-20 06:18:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让它,如果你有一个词“人”,例如,它会像这样。如果用户键入“m”,它将看起来像m\uuu。我知道我的问题在于for循环下的“#Where user will type guess”注释 随机导入

user_input = ""
turns = 5

# List of words
print("Welcome to Advanced Hang Man!")
guesses = ["hello"]

# Picks a random word from the list and prints the length of it
random_guesses = (random.choice(guesses))
right_guess = []
wrong_guess = []

# Prints the hidden word in "_" format
hidden_word = "_" * len(random_guesses)
print(hidden_word)

# Where user will type guess
while True:
    user_input = input("Please enter a letter once at a time:")
    user_input = user_input.lower()
    for i in range(len(random_guesses)):
        if user_input == random_guesses[i]:
            print(hidden_words)

Tags: oftheforinputtyperandomwherewill
2条回答

这是我做游戏的尝试。我没有使用你的right_guesses列表,也没有使用wrong-guesses列表,但它具有恒人游戏的基本功能:

user_input = ""

print("Welcome to Advanced Hang Man!")

random_word= 'bicycle'

hidden_word = "_" * len(random_word)

going = True

while going:
    print(hidden_word)
    user_input = input("Please enter a letter once at a time:");
    user_input = user_input.lower()
    for i in range(len(random_word)): 
      if user_input == random_word[i]:
        print ('Letter found!')
        temp = list(hidden_word)
        temp[i] = user_input
        hidden_word = ''.join(temp)
        if (hidden_word == random_word):
          print ('You Won!!! The word was ' + random_word)
          going = False

您需要迭代实际单词并检查right_guesses列表中的字符。如果找不到,在新单词中用_替换字符。下面是实现这一点的示例代码:

>>> my_word = "StackOverflow"
>>> right_guesses = ['s', 'o', 'c']
>>> ' '.join([word if word.lower() in right_guesses else '_'  for word in my_word])
'S _ _ c _ O _ _ _ _ _ o _'

相关问题 更多 >