--- MasterMind ---
Number guessing game
The player enters 4 digit number, and the computer tells the player how many(but not which) of the 4 digits are correct.
Number changes randomly every time, when new game starts.
E.g.
Guess the number in few tries as possible
Enter the number : 1234
Enter the number : 4567
*
Enter the number : 7533
***
Enter the number : 7593
you used 25 chances, to guess.
CODE
#imports required for validation, i was using older python version for print, i used below import
from __future__ import print_function
import re
from random import randint
print ("Guess the number in few tries as possible\n");
number=[]
inp=str(randint(0000,9999))
for x in inp:
number.append(x)
check = True
count=0
#loop until guess is correct
while check == True:
guess=[]
input=raw_input("\nEnter the number :\t");
count+=1
#validation if the input is numeric
if not re.match("^[0-9]*$", input):
print ("\nError! Only numeric value allowed")
continue
# validation if the number is 4 digit longs or not
if len(input) != 4:
print ("\nError! Must be 4 digit long number")
continue
for x in input:
guess.append(x)
if guess == number:
check = False
break
for i in range(4):
if number[i] == guess[i]:
print ("*", end='')
print ("\nYou used {0} chances, to guess".format(count))
No comments:
Post a Comment