设为首页收藏本站

Crossin的编程教室

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

猜密码游戏

[复制链接]

0

主题

0

好友

12

积分

新手上路

Rank: 1

31#
发表于 2016-3-29 15:54:17 |只看该作者

  1. # 猜数字游戏
  2. import random
  3. def create_code():
  4.   codenum = [1,2,3,4,5,6,7,8,9,0]
  5.   code = ''
  6.   i = random.randint(0,9)
  7.   while True:
  8.     if 4 == len(code):
  9.       break
  10.     if str(codenum[i]) in code:
  11.       i = random.randint(0,9)
  12.     else:
  13.       code += str(codenum[i])
  14.   return code

  15. def validate_code(code, guess_code):
  16.   aNum = 0
  17.   bNum = 0
  18.   if len(code) != len(guess_code):
  19.     print("请输入【4位数字】密码")
  20.     return False
  21.   # elif type(guess_code) != type(0):
  22.   #   print('请输入数字')
  23.   #   return False
  24.   else:
  25.     for i in range(len(code)):
  26.       if code[i] == guess_code[i]:
  27.         aNum += 1
  28.       elif guess_code[i] in code:
  29.         bNum += 1
  30.   if aNum == 4 and bNum == 0:
  31.     print("恭喜你,猜对了",end = '')
  32.     print(aNum,'A',bNum,'B')
  33.     return True
  34.   else:
  35.     print(aNum,'A',bNum,'B')
  36.     return False

  37. def guess_game():
  38.   code = create_code()
  39.   print('让我们来猜一个4位数密码')
  40.   while True:
  41.     guesscode = input("请输入4位数字密码")
  42.     if validate_code(code,guesscode) :
  43.       break
  44.     else:
  45.       continue

  46. guess_game()
复制代码
看了test777的代码才知道random.sample(numbers,pwd_length)函数,惭愧。。
自学Python,不太久,加油
回复

使用道具 举报

0

主题

0

好友

18

积分

新手上路

Rank: 1

32#
发表于 2016-4-13 14:07:16 |只看该作者
本帖最后由 Lisp 于 2016-4-13 14:08 编辑
  1. #!/usr/bin/env python
  2. # coding:utf8
  3. # writen by Lisp at 16/4/13

  4. # import random

  5. # 初始化密码,可以用随机来取,这里先固定
  6. # str1 = random.sample(range(10), 4)
  7. str1 = '1234'

  8. print '请输入四位不重复的数字密码'
  9. while True:
  10.     A = 0
  11.     B = 0
  12.     str2 = raw_input('> ')
  13.     b = [x for x in str2 if str2.count(x) > 1]
  14.     if len(str2) == 4 and str2.isdigit() and not b:
  15.         for i in range(len(str1)):
  16.             # print i
  17.             if str1[i] == str2[i]:
  18.                 A += 1
  19.             elif str1[i] in str2:
  20.                 B += 1
  21.         print '{0}A{1}B'.format(A, B)
  22.         if A == 4 and B == 0:
  23.             print 'Bingo!'
  24.             exit(0)
  25.     else:
  26.         print '输入错误! 请输入四位不重复的数字密码.'
复制代码
回复

使用道具 举报

0

主题

0

好友

18

积分

新手上路

Rank: 1

33#
发表于 2016-4-13 14:08:05 |只看该作者
刚看到这个板块的内容,非常棒!以后就经常来这里训练了
回复

使用道具 举报

0

主题

0

好友

18

积分

新手上路

Rank: 1

34#
发表于 2016-4-13 14:25:38 |只看该作者

这次加上随机的,自己做了一遍,写了20多组才得到正确答...

  1. #!/usr/bin/env python
  2. # coding:utf8
  3. # writen by Lisp at 16/4/13

  4. import random

  5. # 初始化密码,随机初始化
  6. str1 = random.sample(range(10), 4)
  7. str1 = ''.join(map(lambda m: str(m), str1))

  8. print '请输入四位不重复的数字密码'
  9. while True:
  10.     A = 0
  11.     B = 0
  12.     str2 = raw_input('> ')
  13.     b = [x for x in str2 if str2.count(x) > 1]
  14.     if len(str2) == 4 and str2.isdigit() and not b:
  15.         for i in range(len(str1)):
  16.             # print i
  17.             if str1[i] == str2[i]:
  18.                 A += 1
  19.             elif str1[i] in str2:
  20.                 B += 1
  21.         print '{0}A{1}B'.format(A, B)
  22.         if A == 4 and B == 0:
  23.             print 'Bingo!'
  24.             exit(0)
  25.     else:
  26.         print '输入错误! 请输入四位不重复的数字密码.'
复制代码
回复

使用道具 举报

1

主题

0

好友

47

积分

新手上路

Rank: 1

35#
发表于 2016-4-15 22:41:14 |只看该作者
import random
l = [0,1,2,3,4,5,6,7,8,9]
du = ""
for i in range(4):
    ff = random.choice(l)
    du += str(ff)
    l.remove(ff)
print("答案是"+ du )
def guess(du):
    you = input("请输入四位数 不能重复")
    if you == du:
        print("good")
    else :
        right_nume = 0
        wrong_nume = 0
        i = 0
        while i < 4:
            if you[i] in du:
                if you[i] == du[i]:
                    right_nume += 1
                else :
                    wrong_nume += 1
            i += 1
        print(str(right_nume) + "A" + str(wrong_nume) + "B")
        guess(du)
guess(du)
回复

使用道具 举报

0

主题

0

好友

26

积分

新手上路

Rank: 1

36#
发表于 2017-1-23 10:25:28 |只看该作者
import random

s = range(10)
password = random.sample(s,4)
password = list(map(str,password))
#print(password)
dic_password = dict(list(enumerate(password)))
#print(dic_password)
win = False
while win ==False:
    guess = input("guess the password(4 digtals):")
    dic_guess = dict(list(enumerate(list(guess))))
    #print(dic_guess)
    A = 0
    B = 0
    for i in dic_password:
        for j in dic_guess:
            if i == j:
                    if dic_password[i] == dic_guess[j]:
                        A += 1
            else:
                    if dic_password[i] == dic_guess[j]:
                        B += 1

    result = str(A)+"A"+str(B)+"B"
    print(result)
    if result == "4A0B":
        win = True
        print("you win!")





回复

使用道具 举报

1

主题

0

好友

45

积分

新手上路

Rank: 1

37#
发表于 2017-3-8 06:22:38 |只看该作者
看完教程写的,比较初级。。
不知有什么方便的方法复制代码到留言区?

#!/usr/bin/python

import re

passwd='1234'
#for i in passwd:
#  passwd_li.append(i)

guess=input('Please enter your guess: ')
guess_str=str(guess)
A=0
B=0
times=0

if re.findall(r'\d{4}',guess_str):
  guess_li=[]
  for i in guess_str:
    guess_li.append(i)

  for i in guess_str:
    if i==passwd[times]:
      A+=1
      times+=1
    elif re.findall(i,passwd):
      B+=1
      times+=1
    else:
      times+=1

  print '%dA%dB' % (A,B)

else:
  print 'Please input 4 numbers.'


回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

38#
发表于 2017-3-8 17:21:17 |只看该作者
huiwenwu 发表于 2017-3-8 06:22
看完教程写的,比较初级。。
不知有什么方便的方法复制代码到留言区?

留言框上有一个 <> 标志,可以添加代码
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

1

主题

0

好友

45

积分

新手上路

Rank: 1

39#
发表于 2017-3-9 06:45:37 |只看该作者
crossin先生 发表于 2017-3-8 17:21
留言框上有一个  标志,可以添加代码

谢谢~~~
回复

使用道具 举报

0

主题

0

好友

12

积分

新手上路

Rank: 1

40#
发表于 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-4-20 09:55 , Processed in 0.018862 second(s), 20 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部