Skip to content

Instantly share code, notes, and snippets.

@mazrean
Last active April 9, 2020 06:37
Show Gist options
  • Save mazrean/03c98dba595a3705f32909485e113336 to your computer and use it in GitHub Desktop.
Save mazrean/03c98dba595a3705f32909485e113336 to your computer and use it in GitHub Desktop.
python3で書いたnumeron
import random
def is_continue():
while True:
print("do you continue?(y/n):")
ANS = input()
if ANS == "y":
return True
elif ANS == "n":
print("Bye")
return False
else:
print("invalid input")
def get_dig():
while True:
print("please input number of digits(min 1, max 10):")
STR_DIG = input()
try:
DIG = int(STR_DIG)
except ValueError:
print("your input is not number")
if not is_continue():
return -1, True
continue
if DIG not in range(1, 10 + 1):
print("your input is not in specification range")
if not is_continue():
return -1, True
continue
return DIG, False
def make_ans(dig):
STR_RANGE = list(map(str,range(10)))
ANS = random.sample(STR_RANGE, dig)
return ANS
def get_res(dig):
while True:
print("please input {0}-digit number"\
"(if you want to Interrupt,please input `n`):".format(dig))
STR_RES = input()
if STR_RES == "n":
print("Bye")
return [], True
try:
int(STR_RES)
except ValueError:
print("your input is not number")
continue
if len(STR_RES) != dig:
print("the digit of your input is invalid")
continue
elif len(set(STR_RES)) != dig:
print("your input is duplicated")
continue
return list(STR_RES), False
def check_res(res,ans):
bite = 0
eat = 0
for i,v in enumerate(res):
if v == ans[i]:
eat += 1
elif v in ans:
bite += 1
return bite,eat
def numeron():
while True:
DIG,end = get_dig()
if end:
return
ANS = make_ans(DIG)
turn = 1
while True:
print("turn:{0}".format(turn))
RES,err = get_res(DIG)
if err:
return
BITE,EAT = check_res(RES,ANS)
if EAT == DIG:
print("congraturation!!!!\n"\
"you got correct answer at {0} times".format(turn))
break
print("BITE {0} EAT {1}".format(BITE,EAT))
turn += 1
if not is_continue():
turn = 1
return
continue
numeron()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment