设为首页收藏本站

Crossin的编程教室

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

猜密码游戏

[复制链接]

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

好友

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

好友

36

积分

新手上路

Rank: 1

30#
发表于 2016-3-29 12:41:33 |只看该作者
crossin先生 发表于 2016-3-28 17:34
函数内部的end_try和外面的变量end_try不是同一个,具体原因参见后面关于作用域的课程。
你现在的做法, ...

好的呢,我再继续研究一下,谢谢C老师指点了。
回复

使用道具 举报

174

主题

45

好友

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

29#
发表于 2016-3-28 17:34:35 |只看该作者
shangyaoshi 发表于 2016-3-28 09:55
C老师,绝对新手请教啊,为什么我这个代码,即使输出了4A0B,也结束不了呢,还是会继续让猜数字,而不会输 ...

函数内部的end_try和外面的变量end_try不是同一个,具体原因参见后面关于作用域的课程。
你现在的做法,应该是判断compare的返回值是否为 4a0b
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

36

积分

新手上路

Rank: 1

28#
发表于 2016-3-28 09:55:31 |只看该作者
  1. import random

  2. end = '4A0B'
  3. end_try = ''
  4. lst_password = []

  5. def creat_password():
  6.        
  7.         for i in range(4):
  8.                 x1 = random.randint(0,9)
  9.                 lst_password.append(x1)
  10.        
  11.         #print 'the password is: ' , lst_password
  12.         return lst_password
  13.        
  14. def compare():
  15.        
  16.         number_A = 0
  17.         number_B = 0
  18.         print 'Now, please guess the password!'
  19.         for i in range(0,4):
  20.                
  21.                 number = int(raw_input('Enter the number(0-9): '))
  22.                
  23.                 if number == lst_password[i]:
  24.                         number_A += 1
  25.                 else:       
  26.                         if number in lst_password:
  27.                                 number_B += 1
  28.                         else:
  29.                                 pass                       
  30.         end_try = '%dA%dB'        % (number_A,number_B)       
  31.         print end_try
  32.         return end_try

  33.        
  34. print 'Start Game!'
  35. creat_password()
  36. while end_try != '4A0B':
  37.         compare()
  38. print 'You got the right password.Congratulitions!'       
复制代码
C老师,绝对新手请教啊,为什么我这个代码,即使输出了4A0B,也结束不了呢,还是会继续让猜数字,而不会输出最后“You got the right password. Congratulitions!”
回复

使用道具 举报

0

主题

0

好友

12

积分

新手上路

Rank: 1

27#
发表于 2016-3-14 17:10:14 |只看该作者
  1. import re,random
  2. defaultPwd = []
  3. i=0
  4. while i<4:
  5.     defaultPwd.append(str(random.randint(0,9)))
  6.     i += 1
  7. print defaultPwd
  8. def you_guess():
  9.     password = raw_input('请输入密码:')
  10.     countA=0
  11.     countB=0
  12.     listPwd = list(password)
  13.     if re.match(r"\d{4}$", password) and True or False:
  14.         for m in listPwd:
  15.             if m in defaultPwd:
  16.                 if m == defaultPwd[listPwd.index(m)]:
  17.                     countA += 1
  18.                 else:
  19.                     countB += 1
  20.         if '%dA%dB'%(countA,countB) != '4A0B':
  21.             print '%dA%dB'%(countA,countB)
  22.             you_guess()
  23.         else:
  24.             print '密码输入正确'
  25.     else:
  26.         print '请输入正确的密码'

  27. you_guess()
复制代码
回复

使用道具 举报

1

主题

0

好友

207

积分

中级会员

Rank: 3Rank: 3

26#
发表于 2016-1-16 15:19:33 |只看该作者
  1. import random

  2. pwd = ""

  3. for i in range(0,4): pwd += str(random.randint(0,9))

  4. print("Guess a number: ")
  5. print("(",pwd,")")
  6. while True:
  7.   guess = input()
  8.   if len(guess) != 4: continue
  9.   A = 0
  10.   B = 0
  11.   for i in range(0,4):
  12.     B += guess[i] in pwd[i:4]
  13.     A += guess[i] == pwd[i]
  14.   
  15.   print(A,"A",(B - A),"B",sep="")
  16.   if A == 4: break

  17. print("You Win!")
复制代码
回复

使用道具 举报

0

主题

0

好友

22

积分

新手上路

Rank: 1

25#
发表于 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"       
复制代码
回复

使用道具 举报

0

主题

0

好友

8

积分

新手上路

Rank: 1

24#
发表于 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...
回复

使用道具 举报

0

主题

0

好友

42

积分

新手上路

Rank: 1

23#
发表于 2014-12-30 13:37:22 |只看该作者
  1. import random
  2. s = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
  3. p = random.sample(s, 4)
  4. c = True
  5. while c:
  6.         l = raw_input('Please input your guess(4 numbers): ')
  7.         g = [l[0], l[1], l[2], l[3]]
  8.         B = 0
  9.         a = 0
  10.         for i in g:
  11.                 for j in p:
  12.                         if i == j:
  13.                                 B += 1
  14.         for m, n in zip(g, p):
  15.                 if m == n:
  16.                         a += 1
  17.         print '%dA%dB' % (a, B-a)
  18.         if a == 4:
  19.                 print 'You get it!'
  20.                 c = False
复制代码
自己写的一个 貌似用的方法有些诡吊 基本还是面向过程的思想
运行了几次还都正确 不知道是否可以这么干
回复

使用道具 举报

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

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

GMT+8, 2024-5-18 21:12 , Processed in 0.020817 second(s), 20 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部