设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

猜密码游戏

[复制链接]

0

主题

0

好友

8

积分

新手上路

Rank: 1

楼主
发表于 2015-1-14 19:09:07 |显示全部楼层
#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...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-2 09:27 , Processed in 0.016105 second(s), 21 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部