1

Currently designing a mastermind game in Python and the requirement is to guess 4 of the colors the system has randomized out of the 6. Is there a way to only choose 4 character strings from the random.shuffle list of 6 characters? When it runs the random right now, it will choose all 6. I have tried the K=4 function but it doesn't work for the random.shuffle function.

import random

def dashes():
    print("-----------------------------------------------------------------------")

def mainPage():
    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), Pink (p), Orange (o) ")
    print()
    print()


repeat = 'y'
while repeat == 'y' or repeat =='Y':
    print()
    mainPage()
    
    colorCode = ['r','b','g','y','p','o']
    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 ! ")
        print()
        repeat = input("Do you DARE to challenge the Master Mind again? You won't be so lucky the second time(Y/N): ")
        dashes()
    else:
    
        counter = 0

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

        repeat = input("Do you DARE to challenge the Master Mind again? (Y/N) : ")
        dashes()
        print()
print()   
print(" - P R O G R A M    E N D E D - ")


Beron Tan
  • 11
  • 2
  • [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Danila Ganchar Jun 30 '21 at 15:43
  • but why you use `shuffle` ? If you want to take 4 random elements just use `random.sample(colorCode, 4)` – Take_Care_ Jun 30 '21 at 15:44
  • ah @Take_Care_ i didnt know that function exists. My class hasnt covered that yet. I changed it to keycode = random.sample(colorCode, 4) and it fixed it thank you! – Beron Tan Jun 30 '21 at 15:48
  • shuffle would work as well - siply use `chosen_ones = colorCode[:4]` after shuffling - you need to reshuffle every time you need new chosen_ones – Patrick Artner Jun 30 '21 at 15:49
  • Ah @PatrickArtner I tried what you mentioned as well, and both works. Thank you so much for the guidance. – Beron Tan Jun 30 '21 at 16:02

0 Answers0