请选择 进入手机版 | 继续访问电脑版
设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生

猜密码游戏

[复制链接]

0

主题

0

好友

34

积分

新手上路

Rank: 1

发表于 2014-11-25 13:37:39 |显示全部楼层
crossin先生 发表于 2013-9-8 16:02
Good Job!
运行了下,挺好的!

C老师,这个朋友的代码正确吗,我全粘贴上去的也运行不了,到提示我输入数字的时候,我输入以后就什么也没有了
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2014-11-26 19:26:42 |显示全部楼层
z344618505 发表于 2014-11-25 13:37
C老师,这个朋友的代码正确吗,我全粘贴上去的也运行不了,到提示我输入数字的时候,我输入以后就什么也 ...

直接粘贴,大概格式上会有问题吧
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

0

主题

0

好友

42

积分

新手上路

Rank: 1

发表于 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
复制代码
自己写的一个 貌似用的方法有些诡吊 基本还是面向过程的思想
运行了几次还都正确 不知道是否可以这么干
回复

使用道具 举报

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...
回复

使用道具 举报

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"       
复制代码
回复

使用道具 举报

1

主题

0

好友

207

积分

中级会员

Rank: 3Rank: 3

发表于 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

好友

12

积分

新手上路

Rank: 1

发表于 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()
复制代码
回复

使用道具 举报

0

主题

0

好友

36

积分

新手上路

Rank: 1

发表于 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!”
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 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

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

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

使用道具 举报

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

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

GMT+8, 2024-3-29 04:22 , Processed in 0.026223 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部