色彩大师

Mastermind with colors

我正在制作游戏,我不明白为什么当我输入正确的值时,它不会打印获胜文本

例如,为了测试我会输入正确的随机颜色,但在我输入正确答案后什么也不会显示。

其他一切打印正常。

import random

print()
print("                        M A S T E R    M I N D                          ")
print("                            ---------------                             ")
print()
print("The rules to this Master Mind game is simple. You have to guess the 4 COLORS that I am thinking correctly.")
print()
print("                             R U L E S !          ")
print("                               ------             ")
print()
print(" 1. YOU only need to guess 4 colors. ")
print()
print(" 2. Please key in the correct keyword for each color. ( Without spaces )")
print("    E.g rbgy ")
print()
print(" 3. List of colors: Red (r), Blue (b), Green (g), Yellow (y). ")
print()
print()

colorCode = ['r','b','g','y']
random.shuffle(colorCode)
print(colorCode)

print()
print()

playerGuess = str(input("Guess the color: "))
playerGuess = playerGuess.lower()

print ()

if (playerGuess == colorCode):
    print(" You truly are a M A S T E R    M I N D . ")
else:
    
    counter = 0

    while (playerGuess != colorCode):
        counter += 1
        
        count = 0
        
        correct = ['X']*4

        for p in range(0,4):
            
            if(playerGuess[p] == colorCode[p]):
                count += 1
                correct[p] = playerGuess[p]

            else:
                continue

    
        if (count < 4) and (count != 0):
            print("Close! But you did get", count, "correct!")
            print("These are the correct guesses. Keep going!")
            print()
            for k in correct:
                print(k, end='')
            print()
            print()
            playerGuess = input("Guess the color: ")
            playerGuess = playerGuess.lower()
            

        elif (count == 0):
            print("None of your guess are close!")
            playerGuess = input("Guess the color: ")
            playerGuess = playerGuess.lower()
            

    if playerGuess == colorCode:
        print("You win!")
        print("It took you", counter, "tries")
如果您想根据 colorCode = ['r','b','g','y']

检查您的输入,

playerGuess == colorCode 是不正确的

您应该使用 if playerGuess in colorCode 来检查列表包含(单个字符)或 playerGuess == ''.join(colorCode) 如果您想检查打乱后的列表作为字符串