设为首页收藏本站

Crossin的编程教室

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

猜密码游戏

[复制链接]

0

主题

0

好友

22

积分

新手上路

Rank: 1

楼主
发表于 2015-5-6 14:47:01 |显示全部楼层
Python新手
  1. import random
  2. import re

  3. com1=random.sample(range(0,9),4)
  4. com=''.join(str(i) for i in com1)
  5. #print com

  6. while True:
  7.         A=0
  8.         B=0
  9.         print "Please input four number:(input the 'end' quit the game)"
  10.         you=raw_input()
  11.         if you=='end':
  12.                 break
  13.         else:
  14.                 m=re.findall(r"\b\d{4}\b",you)
  15.                 if m:
  16.                         for i in range(4):
  17.                                 for j in range(4):
  18.                                         if i==j:
  19.                                                 if you[i]==com[j]:
  20.                                                         A+=1
  21.                                         else:
  22.                                                 if you[i]==com[j]:
  23.                                                         B+=1
  24.                         print '%dA%dB'%(A,B)
  25.                         if A==4:
  26.                                 print 'you are win !'
  27.                                 break
  28.                         else:
  29.                                 print 'go on ... !'               
  30.                        
  31.        
  32.                 else:
  33.                         print "You input the numbers does't conform to the rules"       
复制代码
回复

使用道具 举报

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

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

GMT+8, 2024-5-2 15:02 , Processed in 0.015764 second(s), 21 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部