【问题标题】:Python - Hangman Game with multiple letters in one wordPython - 一个单词中有多个字母的刽子手游戏
【发布时间】:2014-03-01 12:42:19
【问题描述】:

我是 python 新手,并且对我正在制作的刽子手游戏有疑问。所以我有一个名为“当前”的列表,其中包含连字符 (-) 的次数与人们猜测的单词的长度相同。当他们正确猜出一个字母时,它会将连字符替换为正确位置的字母。我遇到的问题是,如果一个单词中有 2 个或更多相同的字母,那么它在第一次出现该字母时有效,但之后它会说该字母已经被猜到,因此它不起作用,我无法弄清楚如何解决此问题。

current = "_" * len(theword)
x = list(current)
print (x)

guessed = []

while current != theword and lives > 0:

    print ("You have %d lives left" % lives)
    guess = input("Please input one letter or type 'exit' to quit.")
    guess = guess.lower()


    if guess == "exit":
        break
    elif guess in guessed:

        print ("You have already guessed this letter, please try again.")



    guessed.append(guess)

    if guess in theword:
        index = theword.find(guess)
        x = list(current)
        x[index] = guess
        current = "".join(x)
        print ("Correct! \nYour guesses: %s" % (guessed))
        print(x)


    else:
        print ("Incorrect, try again")
        lives = lives -1


if current == theword:
    print ("Well done, you have won!")

谢谢

【问题讨论】:

    标签: python


    【解决方案1】:

    通过使用 string.find 方法,您只能获得单词中第一次出现的字符。通过定义这个方法,你可以得到所有的出现:

    def find_all(word, guess):
        return [i for i, letter in enumerate(word) if letter == guess]
    

    你还应该在检查你之前是否已经猜到这个字母之后添加一个“继续”,这样你就不会再次将它添加到列表中。

    这应该可行:

    def find_all(word, guess):
        return [i for i, letter in enumerate(word) if letter == guess]
    
    current = "_" * len(theword)
    x = list(current)
    print (x)
    
    guessed = []
    
    while current != theword and lives > 0:
    
        print ("You have %d lives left" % lives)
        guess = input("Please input one letter or type 'exit' to quit.")
        guess = guess.lower()
    
    
        if guess == "exit":
            break
        elif guess in guessed:
            print ("You have already guessed this letter, please try again.")
            continue
    
        guessed.append(guess)
    
        if guess in theword:
            indices = find_all(theword, guess)
            x = list(current)
            for i in indices:
                x[i] = guess
                current = "".join(x)
                print ("Correct! \nYour guesses: %s" % (guessed))
                print(x)
    
        else:
            print ("Incorrect, try again")
            lives = lives -1
    

    【讨论】:

      【解决方案2】:

      代替:

      if guess in theword:
              index = theword.find(guess)
              x = list(current)
              x[index] = guess
              current = "".join(x)
              print ("Correct! \nYour guesses: %s" % (guessed))
              print(x)
      

      试试这个:

      for index,character in enumerate(theword):
          x = list(current)
          if character == guess:
              x[index] = guess
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-19
        • 1970-01-01
        相关资源
        最近更新 更多