设为首页收藏本站

Crossin的编程教室

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

猜密码游戏

[复制链接]

0

主题

0

好友

12

积分

新手上路

Rank: 1

楼主
发表于 2017-3-12 18:40:34 |显示全部楼层
这个是我的实现方案
  1. from random import randint

  2. passwdInt = randint(1000,9999)
  3. passwd = str(passwdInt)
  4. print(passwd)
  5. while True:
  6.     # print("请输入猜测的密码:")
  7.     inputpassWd = input()
  8.     if len(inputpassWd) == 0:
  9.         continue
  10.     rightPosCount = 0
  11.     rightNumberCount = 0
  12.     char = ''
  13.     for i in range(len(inputpassWd)):
  14.         char = inputpassWd[i]
  15.         if char == passwd[i]:
  16.             rightPosCount += 1
  17.         else:
  18.             if char in passwd:
  19.                 rightNumberCount += 1

  20.     if rightPosCount== 4 and rightNumberCount == 0:
  21.         print('结果正确:' + str(rightPosCount) + 'A' + str(rightNumberCount) + 'B')
  22.         break
  23.     else:
  24.         print('结果错误:' + str(rightPosCount) + 'A' + str(rightNumberCount) + 'B')
复制代码
回复

使用道具 举报

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

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

GMT+8, 2024-5-9 01:37 , Processed in 0.024929 second(s), 23 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部