python - python 3.x中的Hangman,找到 secret 单词中字母的位置

标签 python position

print("Welcome to Hangman! Guess the mystery word with less than 6 mistakes!")

words= ['utopian','fairy','tree','monday','blue'] 

i=int(input("Please enter a number (0<=number<10) to choose the word in the list: "))

if(words[i]):
    print("The length of the word is: " , len(words[i]))




guesses=0

while guesses<6:
    guess=input("Please enter the letter you guess: ")


    if(guess in words[i]):
        print("The letter is in the word.")


    else:
        print("The letter is not in the word.")
        guesses=guesses+1

    if guesses==6:

        print("Failure. The word was:" , words[i])

在神秘单词中查找猜测字母的位置时遇到问题。我想要一个输出来显示神秘单词中正确猜测的字母。 前任。神秘词是蓝色的。用户输入“b”。输出为:“该字母在单词中。匹配的字母:b___”

最佳答案

有很多种写法,如果将所有猜测的字母放入名为 guessed 的字符串中,然后计算

''.join(c if c in guessed else '_' for c in words[i])

c 将迭代要猜测的单词 words[i] 的字符。

猜测 else '_' 中的 c if c 位将所有尚未猜测的字符替换为下划线。

''.join() 会将字符重新粘在一起形成字符串。

关于python - python 3.x中的Hangman,找到 secret 单词中字母的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19528512/

相关文章:

html - 如何并排放置多个 div?

python - 在列表中查找与 python 中的值比较的第一个最大值的索引

python - 有没有办法在 Python 中为最小值和最大值返回自定义值?

python - 当 y_pred 和 y_true 具有不同大小时的 precision 、 recall 、 f 分数

html - iframe定位

IE8的jquery浏览器检测

python - 如何在gevent中捕获回溯

python - 使用boto从S3逐行读取文件?

javascript - 在javascript中更改元素及其父元素的位置

css - 连续 3 个 div - 无法让最右边的 div 对齐