设为首页收藏本站

Crossin的编程教室

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

猜密码游戏

[复制链接]

0

主题

1

好友

132

积分

注册会员

Rank: 2

楼主
发表于 2013-9-25 17:12:33 |显示全部楼层
本帖最后由 aresli 于 2013-9-25 17:28 编辑
  1. # -*- coding: utf-8 -*-

  2. import random

  3. def create_password():
  4.     L = [0,1,2,3,4,5,6,7,8,9]
  5.     password_length = 4
  6.     passowrd = ''
  7.     i = 0
  8.     while i < password_length:
  9.         l = random.choice(L)
  10.         #print l
  11.         passowrd += str(l)
  12.         L.remove(l)
  13.         #print L
  14.         i += 1
  15.                 return passowrd
  16.         
  17. password = create_password()
  18. print '悄悄告诉你答案是: ' + password

  19. def guess(password):
  20.     you_input = raw_input('请输入4位存数字,不能重复: ')

  21.     if you_input == password:
  22.         print "Bingo"
  23.     else:
  24.         #print "Opoo.."
  25.         right_number_right_position = 0
  26.         right_number_wrong_position = 0
  27.         i = 0
  28.         while i < 4:
  29.             if you_input[i] in password:
  30.                 if you_input[i] == password[i]:
  31.                     right_number_right_position += 1
  32.                 else:
  33.                     right_number_wrong_position += 1
  34.             i += 1
  35.         print str(right_number_right_position) + 'A' + str(right_number_wrong_position) + 'B'
  36.         guess(password)

  37. guess(password)

  38. # vim:et:ts=4:sw=4:
复制代码
测了几次,好像应该是对的!
回复

使用道具 举报

0

主题

1

好友

132

积分

注册会员

Rank: 2

沙发
发表于 2013-9-25 23:25:39 |显示全部楼层
crossin先生 发表于 2013-9-25 20:29
蛮好的。应该是对的

第17行可能是你复制到网页的时候缩进出了问题吧

这个拼写错的问题真是。。。。。
回复

使用道具 举报

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

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

GMT+8, 2024-5-3 21:50 , Processed in 0.022796 second(s), 21 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部