Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mastermind game in python
#1
First year student here trying to code a Mastermind game in python (3.9.4) and the ending code doesnt seems to print the winning messages (As in after multiple false guesses, once you get the correct guess, the win with counter doesn't print). The first guess will print the winning message just fine. It seems that line 73 onwards just gets ignored when the correct guess is entered. May I know what is my mistake or at least a guidance in the right direction to help improve my skills.

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 list(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 list(playerGuess) == colorCode:
        print("You win!")
        print("It took you", counter, "tries")

print()   
print("Program ended") 
Reply


Messages In This Thread
Mastermind game in python - by veronwltan - Jun-16-2021, 05:02 PM
RE: Mastermind game in python - by bowlofred - Jun-16-2021, 05:05 PM
RE: Mastermind game in python - by veronwltan - Jun-16-2021, 05:35 PM
RE: Mastermind game in python - by proebba - Mar-22-2023, 12:03 PM
RE: Mastermind game in python - by proebba - Mar-22-2023, 11:48 AM
RE: Mastermind game in python - by bowlofred - Mar-22-2023, 08:09 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020