【笔记】猜数字
猜一个四位数字,四位数字各不相同,每猜错一次会得到反馈,Golden提示有几位数字是存在且位置正确的,Blue提示有几位是存在但位置错误的。例如答案是8346时,若猜3456,会提示”Golden1,Blue2“,因为“3”和“4”是存在但位置不正确,“6”是存在且位置正确。import random
def diff(a):
b = []
for each in range(4):
b.append(a%10)
a = a//10
if b==b or b==b or b==b or b==bor b==bor b==b:
return False
else:
return True
def check(a,b):
golden = 0
blue = 0
c = []
d = []
for i in range(4):
c.append(a%10)
d.append(b%10)
a = a//10
b = b//10
if c==d:
golden = golden+1
for i in range(4):
for j in range(4):
if i!=j and c==d:
blue = blue+1
print("Golden:{0}, Blue:{1}".format(golden, blue))
while True:
a = random.randint(1,1000)
if diff(a):
break
while True:
b = int(input("猜数字:"))
if not diff(b):
print("RULES!")
continue
if b==a:
print("Bingo!")
break
else:
check(a,b)
页:
[1]