本帖最后由 ivor 于 2017-8-19 08:15 编辑
- # -*- coding:utf-8 -*-
- # AB猜数字
-
-
- import re
- import random
-
- def getKey(*args):
- array = [i for i in range(10)]
- random.shuffle(array)
- key = ''
- for i in range(4):
- key = key +str(array[i])
- if key[0] == '0':
- input("here is %s" %key)
- key = getKey()
- return key
-
-
- k = getKey()
- while True:
- A = 0
- B = 0
- # print("===========")
- print("答案:" + k)
- number = input("请输入4位数字:")
- try:
- if int(number) < 0:
- raise
- except Exception as e:
- print("必须为数字")
- continue
- if len(number) != 4:
- print("位数不正确")
- continue
- elif len(set(number)) < 4:
- print("不允许有重复数字")
- continue
- else:
- for i in range(4):
- if k[i] == number[i]:
- A += 1
- elif k.find(number[i]) != -1:
- B += 1
- if A == 4:
- # print("正确")
- k = getKey()
- print("%dA%dB" % (A, B))
复制代码
|