Study, number guessing game in Python

Study, standard input to number guessing game!

test.py



#! F:/Python/python.exe
# -*- coding: utf-8 -*-

import random
import re

#Number of digits
SCALE = 4

#Returns an array of random, unique numbers
def GetRandomNumbers(place):

	seed = range(0, 10)
	random.shuffle(seed)

	result = ""

	for i in range(0, place):
		result = result + str(seed.pop())

	return result

#Compare answers with answers
def CompareSolAns(solution, answer):
	h = 0
	b = 0
	
	for sIndex, sValue in enumerate(answer):
		for aIndex, aValue in enumerate(solution):
			if sValue == aValue :
				if sIndex == aIndex :
					h += 1
				else :
					b += 1
	
	return {"H":h, "B":b, "A":answer}


#Generate answer
solution = GetRandomNumbers(SCALE)

#Loop to the correct answer
count = 0
history = []
while True :
	#Answer input count
	count += 1
	
	#Enter the answer
	inputStr = "Turn "+str(count)+", Input Number:"
	inputStr = inputStr.decode('utf-8')
	ans = raw_input(inputStr)
	
	#Check input with regular expression
	checkMatch = re.match(r'^\d{'+str(SCALE)+'}$', ans)
	
	#If the input does not follow the regular expression, start over
	if checkMatch is None :
		print u"Bad Input"
		continue
	
	#Compare the answer with the correct answer
	result = CompareSolAns(solution, ans)
	
	#If the answer is correct, end
	if result["H"] == SCALE:
		break
	
	#If the answer is incorrect, the results are stacked and displayed, and a loop is displayed.
	history.append(result)
	
	#View results
	print('------------------------------')
	for i, val in enumerate(history) :
		print "Turn "+str(i+1)+"\t"+val["A"]+"\t"+str(val["H"])+"H\t"+str(val["B"])+"B\t"
	print('------------------------------')

print unicode("****** Exactly! ****** "+str(count)+" turn you are seikai")


Recommended Posts

Study, number guessing game in Python
Prime number 2 in Python
Number recognition in images with Python
Implementation of life game in Python
Let's make a number guessing game in your own language!
Project Euler # 7 "1000 1st prime number" in Python
Project Euler # 17 "Number of Characters" in Python
Python & Machine Learning Study Memo ⑥: Number Recognition
Quadtree in Python --2
CURL in python
Python study note_002
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Python study notes _000
Meta-analysis in Python
Unittest in python
Python study note_004
Discord in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Python study note_003
Python study notes _005
Lifegame in Python.
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
Python study notes_001
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Python study day 1
Output the number of CPU cores in Python
Project Euler # 8 "Maximum Product in Number String" in Python
Prime number enumeration and primality test in Python
Make a rock-paper-scissors game in one line (python)
Project Euler # 12 "High Divisibility Triangular Number" in Python
Sorted list in Python
Daily AtCoder # 36 in Python