Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 软件时空 > 软件相关 > Python版猜数字游戏
【标  题】:Python版猜数字游戏
【关键字】:Python
【来  源】:http://blog.chinaunix.net/article.php?articleId=62873&blogId=12325

Python版猜数字游戏

Your Ad Here Python版猜数字游戏,暑假写来玩的...

#一些东西采用了偷懒的处理方法。。。感觉写Python比较舒服的地方是括号
#少,不习惯的地方是错误处理方法与perl,c,shell都不一样。其它,刚开始
#学,还没太多感慨,Python确实值得学,可是用师兄的话来说,我俩在进行
#“速成”式的学习,写一个小程序,帮助记忆一下基本语法吧。

#!/usr/bin/python
import random
orig_num=[11,11,11,11]

def generate_rand():
    i=0
    while i<4:
        rand=random.randrange(0,10)
        if i==0 and rand==0:
           continue
        else:
            for j in range(0,i):
                if rand==orig_num[j]:
                    rand=11
                    break
            if rand<10:
                orig_num[i]=rand
                i=i+1
   # print orig_num
generate_rand()
i=1
while i<9:
    a=0
    b=0
    guess_input=raw_input('Please input the number you guessed: ')
    if len(guess_input)!=4:
        print 'You should input a integer less than 10000 and greater than 1000'
        continue
    try:
        guess=int(guess_input)
    except ValueError:
        print 'You should enter an integer less than 10000 and greater than 1000!'
        continue
    check_guess=[guess/1000,guess%1000/100,guess%100/10,guess%10]
    for j in range(0,4):
        for k in range(0,4):
            if orig_num[j]==check_guess[k]:
                if j==k:
                    a=a+1
                else:
                    b=b+1
    i=i+1
    if a==4:
        print 'You win! ^&^'
        break
    else:
        print '%d A %d B'%(a,b)
if i>8:
    print 'You lose. The number is %d%d%d%d' %(orig_num[0],orig_num[1],orig_num[2],orig_num[3])

用java访问web:【上一篇】
删除前用脚本提示操作:【下一篇】
【相关文章】
  • Python 笔记1:String Literal
  • Python 笔记2:Tuple with automated packing/unpacking
  • Ironpython 0.96 中文支持
  • C++之python函数调用
  • VC嵌入python时debug版lib下载
  • 不可错过的MSDN TV —— IronPython: Python on the .NET Framework (中)
  • 不可错过的MSDN TV —— IronPython: Python on the .NET Framework (上)
  • [Python] 关于异常处理的的几种方法
  • C++ 扩展和嵌入 Python
  • SWIG,把C/C++ API 导出成Java,C#,Python,Ruby,Php的类库
  • 【随机文章】
  • C-JDBC学习总结
  • 利用Squid反相代理缓存实现网站内容静态发布
  • VC++深入详解--之复习笔记(二)
  • 空间变换及其应用(一)----代数几何重温
  • 在linux下VPN-poptop安装与配置过程
  • 俄罗斯方块积木表示
  • 教你巧妙复制网页的文本内容
  • CorelDRAW9高级应用之应用样式
  • 使用windows xp的对话框
  • 几种目前最好的JSP分页技术
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.