Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 计算两个字符串中的字符数时出现问题_Python_Python 3.x - Fatal编程技术网

Python 计算两个字符串中的字符数时出现问题

Python 计算两个字符串中的字符数时出现问题,python,python-3.x,Python,Python 3.x,此函数的目的是比较颜色在代码中出现的次数和在猜测中出现的次数,以最终确定正确猜测了多少颜色。我在当前代码中遇到了一些错误,我相信行数可以大大简化。更具体地说,我认为我把手头的任务复杂化了 有没有人有更好的方法来解决这个问题的建议?我对编程非常陌生,非常感谢 validColors = ("R", "G", "B", "Y", "O", "P") secretCode = "YBGG" guess = "BYYG" def correctColorCount(secretCode, guess

此函数的目的是比较颜色在代码中出现的次数和在猜测中出现的次数,以最终确定正确猜测了多少颜色。我在当前代码中遇到了一些错误,我相信行数可以大大简化。更具体地说,我认为我把手头的任务复杂化了

有没有人有更好的方法来解决这个问题的建议?我对编程非常陌生,非常感谢

validColors = ("R", "G", "B", "Y", "O", "P")

secretCode = "YBGG"
guess = "BYYG"

def correctColorCount(secretCode, guess, validColors):

    count = 0

    numRedGuess = guess.count("R")
    numRedCode = secretCode.count("R")
    sumR = numRedGuess + numRedCode

    numGreenGuess = guess.count("G")
    numGreenCode = secretCode.count("G")
    sumG = numGreenGuess + numGreenCode

    numBlueGuess = guess.count("B")
    numBlueCode = secretCode.count("B")
    sumB = numBlueGuess + numBlueCode

    numYellowGuess = guess.count("Y")
    numYellowCode = secretCode.count("Y")
    sumY = numYellowGuess + numBlueCode

    numOrangeGuess = guess.count("O")
    numOrangeCode = secretCode.count("O")
    sumO = numOrangeGuess + numOrangeCode

    numPurpleGuess = guess.count("P")
    numPurpleCode = secretCode.count("P")
    sumP = numPurpleGuess + numPurpleCode

    if numRedCode == numRedGuess and sumR != 0 and sumR != 1:
        count += 1
        if numRedGuess == 2:
            count += 1
        elif numRedGuess == 3:
            count += 2
        elif numRedGuess == 4:
            count += 3
    elif numRedGuess >= 1 and numRedCode >= 1 and sumR != 0 and sumR != 1:
        count += 1
    if numGreenCode == numGreenGuess and sumG != 0 and sumG != 1:
        count += 1
        if numGreenGuess == 2:
            count += 1
        elif numGreenGuess == 3:
            count += 2
        elif numGreenGuess == 4:
            count += 3
    elif numGreenGuess >= 1 and numGreenCode >= 1 and sumG != 0 and sumG != 1:
        count += 1
    if numBlueCode == numBlueGuess and sumB != 0 and sumB != 1:
        count += 1
        if numBlueGuess == 2:
            count += 1
        elif numBlueGuess == 3:
            count += 2
        elif numBlueGuess == 4:
            count += 3
    elif numBlueGuess >= 1 and numBlueCode >= 1 and sumB != 0 and sumB != 1:
        count += 1
    if numYellowCode == numYellowGuess and sumY != 0 and sumY != 1:
        count += 1
        if numYellowGuess == 2:
            count += 1
        elif numYellowGuess == 3:
            count += 2
        elif numYellowGuess == 4:
            count += 3
    elif numYellowGuess >= 1 and numYellowCode >= 1 and sumY != 0 and sumY != 1:
        count += 1
    if numOrangeCode == numOrangeGuess and sumO != 0 and sumO != 1:
        count += 1
        if numOrangeGuess == 2:
            count += 1
        elif numOrangeGuess == 3:
            count += 2
        elif numOrangeGuess == 4:
            count += 3
    elif numOrangeGuess >= 1 and numOrangeCode >= 1 and sumO != 0 and sumO != 1:
        count += 1
    if numPurpleCode == numPurpleGuess and sumP != 0 and sumP != 1:
        count += 1
        if numPurpleGuess == 2:
            count += 1
        elif numPurpleGuess == 3:
            count += 2
        elif numPurpleGuess == 4:
            count += 3
    elif numPurpleGuess >= 1 and numPurpleCode >= 1 and sumP != 0 and sumP != 1:
        count += 1

    return count

这里有一种方法:

def countColors(word, colors):
    return (word.count(color) for color in colors)

def correctColorCount(secretCode, guess, colors):
    for index, (guessedColor, correctColor) in enumerate(zip(*map(lambda word: 
        countColors(word, colors), (secretCode, guess)))):
        color = colors[index]
        if guessedColor == correctColor:
            print("You guessed", color, "correctly!")
        else:
            print("You guessed", color, "incorrectly. You guessed", guessedColor, "but it was", str(correctColor) + ".")


>>> validColors = ("R", "G", "B", "Y", "O", "P")
>>> secretCode = "YBGG"
>>> guess = "BYYG"
>>> correctColorCount(secretCode, guess, validColors)
You guessed R correctly!
You guessed G incorrectly. You guessed 2 but it was 1.
You guessed B correctly!
You guessed Y incorrectly. You guessed 1 but it was 2.
You guessed O correctly!
You guessed P correctly!
countColors
将计算一个单词中每种颜色的数量,给出一个要计算的颜色列表。这使用列表理解

correctColorCount
说明:

map(lambda单词:countColors(单词,颜色),(secretCode,猜测))

这将使用secretCode和guess运行countColors。等同于写作:

[countColors(secretCode,colors),countColors(guess,colors)]

zip(*…)

这将使它们成对出现:每个颜色的第一个,然后是第二个,等等。因此,这将基本上为每个颜色创建一个列表:
[实际次数,猜测次数]


枚举
可能更容易理解。

也许您可以使用以下方法从类似的内容构建:


这将为您提供正确猜测的计数,而不考虑位置。

哇,这是很多代码。。。可简化为:

def correctColorCount(secretCode, guess):
    guess_list = list(guess)
    return len(guess) - sum(1 for color in secretCode 
                            if color not in guess_list or guess_list.remove(color))

不需要额外的模块<此比较不需要代码>有效颜色,当用户输入其组合时,您应该检查
有效颜色

假设我们忽略两个输入字符串中缺少的颜色,以下是一种方法:

def correctColorCount(secretCode, guess, validColors):
    count = 0
    # for all the color characters
    for color in validColors:
        # if color count is not zero and same for both strings
        if secretCode.count(color) == guess.count(color) != 0:
            count += 1
    return count

这是给你的吗?是的!如果我把程序的其余部分包括进来,会有帮助吗?因为Mastermind是关于计算元素的,试试看,我用在(25行)中。
def correctColorCount(secretCode, guess, validColors):
    count = 0
    # for all the color characters
    for color in validColors:
        # if color count is not zero and same for both strings
        if secretCode.count(color) == guess.count(color) != 0:
            count += 1
    return count