-2

I am making a game and I don't get why when I input the right values, it won't print the winning text

For example, for testing I would input the correct random color, but nothing will display after I input the right answer.

Everything else prints out fine.

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")
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Beron Tan
  • 11
  • 2
  • 2
    [How to debug small programs.](//ericlippert.com/2014/03/05/how-to-debug-small-programs/) | [What is a debugger and how can it help me diagnose problems?](//stackoverflow.com/q/25385173/843953) Step through your code and narrow down the problem into a [mre]. Then ask a specific question. _"Why is my code doing this"_ is [too broad for Stack Overflow.](//meta.stackoverflow.com/a/253788/843953) Please also take the [tour], read [ask] and [what's on-topic](/help/on-topic). Welcome to Stack Overflow! – Pranav Hosangadi Jun 16 '21 at 16:20
  • u can use `list(playerGuess)` in if statement – Suraj Shejal Jun 16 '21 at 16:34
  • Ahh thanks! @SurajS this option worked too! – Beron Tan Jun 16 '21 at 16:39

1 Answers1

2

playerGuess == colorCode is incorrect if you want to check your input against colorCode = ['r','b','g','y']

You should use if playerGuess in colorCode to check list containment (a single character) or playerGuess == ''.join(colorCode) if you wanted to check the shuffled list as a string

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245