- 帖子
- 2
- 精华
- 0
- 积分
- 8
- 阅读权限
- 10
- 注册时间
- 2014-11-30
- 最后登录
- 2015-1-14
|
#coding:utf-8
import string
from random import sample
__author__ = 'Administrator'
'''以前店子词典上有个游戏:要你猜一个4位密码,密码由0-9组成,不重复。才对正确位置上的数字为A,猜到数字但位置不正确为B,每次显示答案中A和B的个数。'''
pa = ['1','2','3','4','5','6','7','8','9','0',]
password = sample(pa,4)
#print password
mypass = '4A0B'
result = ['']*4
while ''.join(result) != mypass:
result = ['']*4
i = 0
j = 0
text = raw_input('Please guess the password of 4 unrepeated numbers 0-9:')
for x,y in zip(text,password):
if x == y:
i += 1
for z in text:
if z in password:
j += 1
result[0] = str(i)
result[1] = 'A'
result[2] = str(j - i)
result[3] = 'B'
print ''.join(result)
else:
print mypass
试了一下,貌似OK... 支持crossin... |
|