Python-猜词游戏在带有重复字符的单词上失败

我是编程新手,但我一直在教自己 Python on and off 几个月。我写了一个猜词游戏程序。该程序执行以下操作:

我是编程新手,但我一直在教自己 Python on and off 几个月。我写了一个猜词游戏程序。该程序执行以下操作:

它从预定义的一组单词中选择一个单词,我称之为dictionary

它解释了游戏规则。

它告诉用户单词中有多少个字母,并用下划线表示每个字母。

它要求用户猜测一个字母。

它告诉用户猜测是正确的,然后它重新打印下划线,但用正确的猜测替换相关的下划线。我的意思是,如果单词是 'PYTHON' 并且用户猜测 'H',它将打印["_", "_", "_", "H", "_", "_"]

另一方面,如果猜测不在单词中,它会告诉用户,然后重新打印下划线。我的意思是,如果用户的下一个猜测是“Z”,它会说“这个字母不在单词中”,然后再次重新打印["_", "_", "_", "H", "_", "_"],这样用户就可以跟踪他 / 她在哪里。

最后,一旦所有的字母都被猜出来,它祝贺用户并结束游戏。

是代码:

import random
dictionary = ("CROWS", "GOAT", "MONKEY", "COW", "HORSE", "SNAKE")
answer = random.choice(dictionary)
answer_length = len(answer)
victory = True
print("""
    I have chosen a word.  Your job is to guess it. I will help you
    keep track.
    """)
print("The word I have chosen is {} letters long.\n".format(answer_length))
underscores = []
for character in answer:
    underscores.append("_")
print(underscores)
while victory:
    guess = input("\nPlease guess a letter: ").strip().capitalize()
    def pos(guess):
        position = 0
        for letter in answer:
            if letter != guess:
                position += 1
            else:
                print("Correct!")
                break
        return position
    def update_board(x):
        global underscores
        if guess in answer:
            underscores[x] = guess
        else:
            print("This letter is not in the word")
        return underscores
    def winner():
        global underscores
        global victory
        if "_" not in underscores:
            print("YOU HE WON!")
            victory = False
    x = pos(guess)
    print(update_board(x))
    winner()

现在,我遇到的问题是:该程序工作正常,提供计算机从字典中选择的单词中没有任何重复的字母。您会注意到字典中的单词都由唯一字符组成。但是,如果字典中包含单词“CHICKEN”,则程序可能会遇到问题。

想象一下,计算机选择了“CHICKEN”,然后您猜到了字母“C”。该程序将返回:

Correct!
["C", "_", "_", "_", "_", "_", "_"]

此外,如果你再次猜测 C,它只会打印

Correct!
["C", "_", "_", "_", "_", "_", "_"]

再次。理想情况下,我希望它做的是返回["C", "_", "_", "C", "_", "_", "_"]用户第一次猜到 'C'。目前,游戏只能工作,如果字典中的所有单词没有任何重复出现的字符。

你能提供的任何帮助将不胜感激。谢谢!

2

一旦你在pos(guess)中找到一个,你就会停下来检查是否有任何进一步的出现。因此,你只得到第一次出现。显然,为了解决这个问题,当你发现一些东西时不要停下来。相反,每次都要完全遍历这个词。

例如,在pos(guess)中,返回在单词中找到字母的索引列表,并在update_board(x)中循环遍历您获得的索引,而不是仅将一个下划线替换为猜测。

1

你可以使用数组来解决它:

import random
dictionary = ("CROWS","CHICKEN")
answer = random.choice(dictionary)
answer_length = len(answer)
victory = True
print("""
    I have chosen a word.  Your job is to guess it. I will help you
    keep track.
    """)
print("The word I have chosen is {} letters long.\n".format(answer_length))
underscores = []
for character in answer:
    underscores.append("_")
print(underscores)
ar=[]
while victory:
    guess = input("\nPlease guess a letter: ").strip().capitalize()
    def pos(guess):
        position = 0
        for letter in answer:
            if letter != guess:
                position += 1
            else:
                print("Correct!")
                ar.append(position)
                position += 1
        return ar
    def update_board(x):
        global underscores
        if guess in answer:
            underscores[x] = guess
        else:
            print("This letter is not in the word")
        return underscores
    def winner():
        global underscores
        global victory
        if "_" not in underscores:
            print("YOU HE WON!")
            victory = False
    pos(guess)
    for x in ar:
        print(update_board(x))
    del ar [:]
    winner()

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(273)
在Docker中使用SpringBootJava生成ApachePOI xlsx文件
上一篇
WindowsServerAD2022-使用PowerShell通过GPO将域用户添加到本地组“远程桌面用户”
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(22条)