【问题标题】:How can I show the guessed letters of a word guesser in order? Python如何按顺序显示单词猜测器的猜测字母? Python
【发布时间】:2019-10-19 20:56:34
【问题描述】:

我被要求在 python 中创建一个单词猜测器,它向用户输出单词中有多少个字母,例如 python 有 6 个,所以它会输出 6。然后用户有 5 个猜测来猜测哪些字母在单词中,在这 5 次猜测之后,用户是为了猜测单词。我已经能够通过将它们连接到一个新字符串来显示正确猜到了哪些字母,但是我无法显示单词的正确位置,并且如果字母在单词中出现两次,基本上就像刽子手.

问题 1:我怎样才能让猜到的字母按照单词的顺序出现,而不是按照它们被猜到的顺序出现?

问题 2:如何让一个重复的字母无论在单词中出现多少次都显示出来?

代码如下:

#WordGuesser

import random

WORDS = ("computer","science","python","pascal","welcome")

word = random.choice(WORDS)
correctLetters = ""
guesses = 0

print(
    """

    Welcome to Word Guesser!
    You have 5 chances to ask if a certain letter is in the word
    After that, you must guess the word!

    """
)

print("The length of the word is", len(word))


while guesses != 5:
    letter = input("Guess a letter: ")

    if letter.lower() in word:
        print("Well done", letter, "is in the word")
        correctLetters += letter
        print("Correctly guessed letters are: ",correctLetters)
        guesses += 1


    else:
        print("No", letter, "is not in the word")
        correctLetters += "-"
        guesses += 1

guess = input("Please now guess a word that it could be!: ")

if guess == word:
    print("Well done, you guessed it")
    input("\n\nPress enter key to exit")

else:
    ("You did not guess it, the word was: ",word)

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    您应该对单词进行迭代,以便输出正确单词中字母的猜测字母。使用一个集合来跟踪正确的字母以进行有效的查找,因为您只需要它来确定一个字母是否被正确猜到:

    correctLetters = set()
    while guesses != 5:
        letter = input("Guess a letter: ").lower()
    
        if letter in word:
            print("Well done", letter, "is in the word")
            correctLetters.add(letter)
            print("Correctly guessed letters are: ", ''.join(letter if letter in correctLetters else '-' for letter in word))
    
        else:
            print("No", letter, "is not in the word")
        guesses += 1
    

    演示:https://repl.it/@blhsing/WellmadeAlarmingInformation

    【讨论】:

    • +1。但是,在添加到集合时不需要测试成员资格 - 循环遍历单词的字母将忽略任何错误猜测的内容。
    • 是的,但既然已经有这样一个条件语句用于其他目的,不妨将add 调用放在条件中,以免它不必要地跟踪不会出现的错误字母显示猜一半的单词时需要。
    • 同意。除了将它放在“猜测正确”的 IF 中有点掩盖了基于集合的方法的简单性。内存大小在这里无关紧要,并且设置成员资格测试非常快。但是,是的,你已经有了你的 IF。
    【解决方案2】:

    这是一个解决方案,不优雅,因为你不能使用函数,但它的工作。

    import random
    
    WORDS = ("computer","science","python","pascal","welcome")
    
    word = random.choice(WORDS)
    correctLetters = ""
    guesses = 0
    
    print(
        """
    
        Welcome to Word Guesser!
        You have 5 chances to ask if a certain letter is in the word
        After that, you must guess the word!
    
        """
    )
    
    print("The length of the word is", len(word))
    
    
    
    while guesses != 5:
        list_word = list(word)
        letter = input("Guess a letter: ")
    
        if letter.lower() in word:
            print("Well done", letter, "is in the word")
            correctLetters += letter
            guess_part = ""
            for word_letter in list_word:
                if word_letter in correctLetters:
                    guess_part += word_letter
                else:
                    guess_part += "*"
            print("Correctly guessed letters are: ", guess_part)
            guesses += 1
    
    
        else:
            print("No", letter, "is not in the word")
            correctLetters += "-"
            guesses += 1
    
    guess = input("Please now guess a word that it could be!: ")
    
    if guess == word:
        print("Well done, you guessed it")
        input("\n\nPress enter key to exit")
    
    else:
        ("You did not guess it, the word was: ",word)
    

    【讨论】:

      猜你喜欢
      • 2013-03-27
      • 2022-10-07
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-31
      • 1970-01-01
      相关资源
      最近更新 更多