Skip to content

Create Mastermind Game using Python #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions Mastermind Game using Python
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import random


# the .randrange() function generates a
# random number within the specified range.
num = random.randrange(1000, 10000)

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

# condition to test equality of the
# guess made. Program terminates if true.
if (n == num):
print("Great! You guessed the number in just 1 try! You're a Mastermind!")
else:
# ctr variable initialized. It will keep count of
# the number of tries the Player takes to guess the number.
ctr = 0

# while loop repeats as long as the
# Player fails to guess the number correctly.
while (n != num):
# variable increments every time the loop
# is executed, giving an idea of how many
# guesses were made.
ctr += 1

count = 0

# explicit type conversion of an integer to
# a string in order to ease extraction of digits
n = str(n)

# explicit type conversion of a string to an integer
num = str(num)

# correct[] list stores digits which are correct
correct = ['X']*4

# for loop runs 4 times since the number has 4 digits.
for i in range(0, 4):

# checking for equality of digits
if (n[i] == num[i]):
# number of digits guessed correctly increments
count += 1
# hence, the digit is stored in correct[].
correct[i] = n[i]
else:
continue

# when not all the digits are guessed correctly.
# if (count < 4) and (count != 0): - this condition is not needed as we are starting with the condition, n!=num, which is, count<4
print("Not quite the number. But you did get ",
count, " digit(s) correct!")
# second code is not supposed to print the guessed numbers, from the sample output, here I get we are not recording the position of the guess,but count. But as per the explanation, the code should not print the guessed numbers, rather give their count.
# print("Also these numbers in your input were correct.")
# for k in correct:
# print(k, end=' ')
print('\n')
print('\n')
n = int(input("Enter your next choice of numbers: "))

# when none of the digits are guessed correctly.
elif (count == 0):
print("None of the numbers in your input match.")
n = int(input("Enter your next choice of numbers: "))

# condition for equality.
if n == num:
# ctr must be incremented when the n==num gets executed as we have the other incrmentation in the n!=num condition
ctr+=1
print("You've become a Mastermind!")
print("It took you only", ctr, "tries.")