Mastermind Number Generator Game

I have created a code using Python for a game that will generate a random 4 digit number and make you guess it.

from random import randint

N1 = randint(1,9)
N2 = randint(1,9)
N3 = randint(1,9)
N4 = randint(1,9)


print('Guess the four digit number (one digit at a time)')

answer = N1,N2,N3,N4
tries = 1
while True:
    g1 = int(input())
    g2 = int(input())
    g3 = int(input())
    g4 = int(input())
    numbersr = 0
    numbersw = 0

    if (g1 == N1):
        numbersr += 1
    else: numbersw += 1

    if (g2 == N2):
        numbersr += 1
    else: numbersw += 1

    if (g3 == N3):
        numbersr += 1
    else: numbersw += 1

    if (g4 == N4):
        numbersr += 1
    else: numbersw += 1

    if (numbersr == 4):
        print('Congratulations, it took you ' + str(tries) + ' tries')
        break
    else:
        print('You got ' + str(numbersr) + ' right')
        tries += 1

After a wrong guess it will tell you how many you guessed right and at the end it will tell you how many tries it took you to get it.

Leave a comment

Design a site like this with WordPress.com
Get started