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
#2
colorCode is a list. playerGuess is a string. On line 33 (and on line 74) you cast the string as a list to do the comparison. But on line 39 you do not. The string will never be equal to the list and the while loop will never exit.
veronwltan likes this post
Reply
#3
(Jun-16-2021, 05:05 PM)bowlofred Wrote: colorCode is a list. playerGuess is a string. On line 33 (and on line 74) you cast the string as a list to do the comparison. But on line 39 you do not. The string will never be equal to the list and the while loop will never exit.

AHH omg! I can't believe I overlooked it. Thank you for pointing out my error. I fixed it and now its working fine! Thanks again so much
Reply
#4
How would this code look if you changed it to numbers? As in you would guess four numbers instead?
Reply
#5
(Jun-16-2021, 05:05 PM)bowlofred Wrote: colorCode is a list. playerGuess is a string. On line 33 (and on line 74) you cast the string as a list to do the comparison. But on line 39 you do not. The string will never be equal to the list and the while loop will never exit.

How would this code look like if you were to guess four numbers instead?
Reply
#6
When you input() from the console, it always comes in as text. So even if the user is typing "3451", you'll get that as a string. So using strings of digits is identical to using strings of letters. Other than changing the colorcode list to have digits, there shouldn't be anything much to do.

If you had an integer for some reason, just convert it to a string and handle it as above.
Reply


Forum Jump:

User Panel Messages

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