Master Mind Game In Python

Home » Coding » Basic Coding » Master Mind Game In Python

The Master Mind game in Python allows the user to experience tricky gameplay. We have developed this application in such a way that it takes input from the user for the set of 4 digits numbers. If the user enters digits similar to the set of digits generated by the code, the loop continues to get the exact same 4-digit values. If the digit is not similar, the loop continues to obtain the right numbers.

Introduction

Our mastermind game is basically a number-guessing game. We have used the random library to generate a random four-digit number that the player needs to guess. This basic Python project uses concepts like loops, conditionals, and type conversion to manage the game flow and provide feedback to the player.

The game encourages continuous interaction from the user by providing feedback on each guess. Feedback includes information on the number of correct digits and their place. When the player successfully guesses the number, the code congratulates them and reveals how many attempts it took.

Objective

The objective of the Master Mind Game in Python is to obtain an application that gives a user-friendly environment for gameplay. In this project, we are creating a game application where the user needs to guess the complete 4-digit number that is needed to win the Master Mind game.

Requirements

1. You must have a basic understanding of Python.

2. Any text editor or Visual Studio Code

3. To run and build the Python code on your machine, you will need a Python Environment.

Source code

import random
num = random.randrange(1000, 10000)


n = int(input("Guess the 4 digit number:"))


if(n == num):
    print("Great! You guessed it in just 1 try! You're a Mastermind!")
else:
       ctr = 0


      while(n != num):
        
        ctr += 1


        count = 0
         n = str(n)


                num = str(num)


                correct = []


                for i in range(0, 4):
                       if(n[i] == num[i]):
                                count += 1
                                correct.append(n[i])
            else:
                continue


               if (count < 4) and (count != 0):
            print("Not quite the number. But you did get ",
                  count, " digit(s) correct!")
            print("You are almost near! try again.")


            for k in correct:
                print(k, end=' ')


            print('\n')
            print('\n')
            n = int(input("Enter your next choice of numbers: "))


               elif(count == 0):
            print("None of the numbers in your input match.")
            n = int(input("Enter your next choice of numbers: "))


    if n == num:
        print("You've become a Mastermind!")
        print("It took you only", ctr, "tries.")

 

Explanation of code

1. The law starts by initializing a variable, ctr, to 0.

2. Next, the law creates an arbitrary number within the range of 1000 and 10000. The. randrange() function generates this arbitrary number. The coming line assigns this arbitrary number to num.

3. Further, the law tests whether num is equal to the aimlessly generated number, num.However, also the program terminates and prints “ Great! You guessed the number in just 1 pass! You ‘re an Architect! ” If they’re equal. else, it continues with the whole circle.

4. The whole circle runs as long as n! = num( which means that n can not be equal to num). Every time prosecution of this circle occurs ctr supplements which give an idea of how numerous suppositions were made. The for circle also runs 4 times since there are 4 integers in n( 0- 3). For each replication of this circle, I in range( 0,4) checks if n( i) == num( i). still, which will store all integers that If they’re equal also count; differently continue is executed and k is stored in correct()( an empty list).

5. The law initializes a variable called ctr which will keep track of how numerous times the player supposes the number. The law also creates a whole circle that will repeat as long as the player fails to guess the number correctly.

6. In each replication of the whole circle, the law supplements a variable called ctr and stores this value in a variable called count. The law also creates a list called correct which will store integers that are correct.

7. Eventually, the law prints out some information about what was entered into the input field and terminates if n == num.

Output

Master Mind Game in Python

Figure 1: Master Mind Game

Master Mind Game in Python

Figure 2: User won the Master Mind Game

Conclusion

Hence, successfully developed the Master Mind Game in Python, which is a tricky game that takes a set of numbers from the user that has been matched to the words of the game design. If the number entered is right, then the user will win, or else will lose the game. In a nutshell, this code presents an engaging guessing game that uses basic Python programming concepts.

More Python Projects>>>

You May Also Like To Create…