python实现猜数字游戏(无重复数字)示例分享


Posted in Python onMarch 29, 2014
import time, random
class GuessNum:
    def __init__(self):
        self._num = ''
        self.input_num = []
        self.count = 1                                      #猜对所用次数
        self.sec = 0                                           #猜对所用时间
        self._generate_num()
    def _generate_num(self):                        #产生不重复的四个数字
        seq_zton = list(range(10))
        for i in range(0, 4):
            a = str(random.choice(seq_zton))   #选出一个数字
            self._num += a
            seq_zton.remove(int(a))                 #注意a的类型
        self.sec = time.clock()                          #开始计时
    def check_answer(self):
        return self._num
    def check_input(self):
        num_pos, num_value = 0, 0               #位置对和数值对的分别的个数
        tmp = input("Please input the number you guess(No repetition),or 'c' to check the answer:")
        if tmp == 'c':
            print(self.check_answer())
            tof = self.check_input()
            return tof
        elif not tmp.isalnum or not len(tmp) == 4:
            print("Wrong format!")
            tof = self.check_input()                #需要优化
            return tof
        self.input_num = list(tmp)
        lst_temp = list(self._num)
        if self.input_num == lst_temp:          #猜对
            self.prt_vic()
            return True
        for i in lst_temp:
            if i in self.input_num:
                if lst_temp.index(i) == self.input_num.index(i):        #位置也相同
                    num_pos += 1
                    num_value += 1
                else:
                    num_value += 1
        self.prt_state(num_pos, num_value)
        self.count += 1
        return False
    def prt_state(self, num_pos, num_value):
        print("You've got %d numbers with the right position and %d numbers with the right value only" % (num_pos, num_value))
    def prt_vic(self):
        t = time.clock()
        self.sec = t - self.sec
        print("Congratulations!You have successfully got the right number!")
        print("%d times and %.2f sec in total to get the right answer" % (self.count, self.sec))
gn = GuessNum()
while True:
    ss = gn.check_input()
    if ss:
        b = input("Continue? y/n:")
        if b == 'n':
            break
        else:
            gn = GuessNum()
            continue
Python 相关文章推荐
在Python编程过程中用单元测试法调试代码的介绍
Apr 02 Python
Python 搭建Web站点之Web服务器与Web框架
Nov 06 Python
解决python3在anaconda下安装caffe失败的问题
Jun 15 Python
Python 将RGB图像转换为Pytho灰度图像的实例
Nov 14 Python
Python初学者需要注意的事项小结(python2与python3)
Sep 26 Python
Python整数对象实现原理详解
Jul 01 Python
Python搭建Spark分布式集群环境
Jul 05 Python
Python使用pyautocad+openpyxl处理cad文件示例
Jul 11 Python
django删除表重建的实现方法
Aug 28 Python
Python3实现二叉树的最大深度
Sep 30 Python
python turtle工具绘制四叶草的实例分享
Feb 14 Python
python 装饰器的实际作用有哪些
Sep 07 Python
使用python实现扫描端口示例
Mar 29 #Python
Python Trie树实现字典排序
Mar 28 #Python
python实现探测socket和web服务示例
Mar 28 #Python
python实现目录树生成示例
Mar 28 #Python
python改变日志(logging)存放位置的示例
Mar 27 #Python
使用python删除nginx缓存文件示例(python文件操作)
Mar 26 #Python
python实现ip查询示例
Mar 26 #Python
You might like
PHP邮件专题
2006/10/09 PHP
PHP安全下载文件的方法
2016/04/07 PHP
yii2使用gridView实现下拉列表筛选数据
2017/04/10 PHP
PHP中关于php.ini参数优化详解
2020/02/28 PHP
csdn 博客中实现运行代码功能实现
2009/08/29 Javascript
高效的表格行背景隔行变色及选定高亮的JS代码
2010/12/04 Javascript
javaScript矢量图表库-gRaphael几行代码实现精美的条形图/饼图/点图/曲线图
2013/01/09 Javascript
javascript学习笔记(五)原型和原型链详解
2014/10/08 Javascript
详解JavaScript对象序列化
2016/01/19 Javascript
静态页面html中跳转传值的JS处理技巧
2016/06/22 Javascript
Angular ng-class详解及实例代码
2016/09/19 Javascript
Javascript中this绑定的3种方法与比较
2016/10/13 Javascript
JS实现类似百叶窗下拉菜单效果
2016/12/30 Javascript
jQuery用户头像裁剪插件cropbox.js使用详解
2017/06/07 jQuery
详解vue项目构建与实战
2017/06/27 Javascript
async/await让异步操作同步执行的方法详解
2019/11/01 Javascript
微信小程序 scroll-view的使用案例代码详解
2020/06/11 Javascript
JavaScript交换变量常用4种方法解析
2020/09/02 Javascript
vue实现滚动鼠标滚轮切换页面
2020/12/13 Vue.js
Python正则表达式介绍
2012/08/06 Python
python实现sublime3的less编译插件示例
2014/04/27 Python
python搭建虚拟环境的步骤详解
2016/09/27 Python
TensorFlow实现Batch Normalization
2018/03/08 Python
python获取代码运行时间的实例代码
2018/06/11 Python
对python 通过ssh访问数据库的实例详解
2019/02/19 Python
Python大批量搜索引擎图像爬虫工具详解
2020/11/16 Python
jupyter使用自动补全和切换默认浏览器的方法
2020/11/18 Python
韩国休闲女装品牌网站:ANAIS
2016/08/24 全球购物
Ruby如何进行文件操作
2014/07/17 面试题
给老师的检讨书
2014/02/11 职场文书
2014小学生国庆65周年演讲稿
2014/09/21 职场文书
2014年计生工作总结
2014/11/21 职场文书
经理助理岗位职责
2015/02/02 职场文书
网聊搭讪开场白
2015/05/28 职场文书
2019年销售人员的职业生涯规划书
2019/03/25 职场文书
springboot + mongodb 通过经纬度坐标匹配平面区域的方法
2021/11/01 MongoDB