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抓取某汽车网数据解析html存入excel示例
Dec 04 Python
用Python编程实现语音控制电脑
Apr 01 Python
Python面向对象编程中关于类和方法的学习笔记
Jun 30 Python
详解Python list 与 NumPy.ndarry 切片之间的对比
Jul 24 Python
Python入门之三角函数atan2()函数详解
Nov 08 Python
python Flask 装饰器顺序问题解决
Aug 08 Python
Tesserocr库的正确安装方式
Oct 19 Python
python 处理string到hex脚本的方法
Oct 26 Python
django-rest-framework解析请求参数过程详解
Jul 18 Python
django-filter和普通查询的例子
Aug 12 Python
让你的Python代码实现类型提示功能
Nov 19 Python
Python3使用 GitLab API 进行批量合并分支
Oct 15 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 Static关键字实用方法
2010/06/04 PHP
PHP中使用正则表达式提取中文实现笔记
2015/01/20 PHP
php实现smarty模板无限极分类的方法
2015/12/07 PHP
ThinkPHP中获取指定日期后工作日的具体日期方法
2018/10/14 PHP
jquery tools之tabs 选项卡/页签
2009/07/25 Javascript
用JavaScript修改CSS属性的代码
2013/05/06 Javascript
jquery的each方法使用示例分享
2014/03/25 Javascript
javascript框架设计读书笔记之种子模块
2014/12/02 Javascript
Jquery实现弹性滑块滑动选择数值插件
2015/08/08 Javascript
全系IE支持Bootstrap的解决方法
2015/10/19 Javascript
JS简单封装的图片无缝滚动效果示例【测试可用】
2017/03/22 Javascript
Angular.JS去掉访问路径URL中的#号详解
2017/03/30 Javascript
详解react使用react-bootstrap当轮子造车
2017/08/15 Javascript
使用mock.js随机数据和使用express输出json接口的实现方法
2018/01/07 Javascript
vue脚手架搭建过程图解
2018/06/06 Javascript
JS扁平化输出数组的2种方法解析
2019/09/17 Javascript
Python处理JSON数据并生成条形图
2016/08/05 Python
如何利用Python模拟GitHub登录详解
2019/07/15 Python
Python 动态导入对象,importlib.import_module()的使用方法
2019/08/28 Python
Python实现使用dir获取类的方法列表
2019/12/24 Python
numpy实现神经网络反向传播算法的步骤
2019/12/24 Python
python实现简单飞行棋
2020/02/06 Python
keras的三种模型实现与区别说明
2020/07/03 Python
HTML利用九宫格原理进行网页布局
2020/03/13 HTML / CSS
StubHub西班牙:购买和出售全球活动门票
2017/06/05 全球购物
世界领先的艺术图书出版社:TASCHEN
2018/07/23 全球购物
七一党建活动方案
2014/01/28 职场文书
家长给小学生的评语
2014/01/30 职场文书
幼儿园保育员岗位职责
2014/04/13 职场文书
股东合作协议书
2014/04/14 职场文书
二年级小学生评语
2014/04/21 职场文书
学校领导四风问题整改措施思想汇报
2014/10/09 职场文书
赔偿协议书
2015/01/27 职场文书
职称评定个人总结
2015/03/05 职场文书
怎样做好公众演讲能力?
2019/08/28 职场文书
Win11怎么添加用户?Win11添加用户账户的方法
2022/07/15 数码科技