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引用DLL文件的方法
May 11 Python
Python构造自定义方法来美化字典结构输出的示例
Jun 16 Python
对python 合并 累加两个dict的实例详解
Jan 21 Python
Python魔法方法功能与用法简介
Apr 04 Python
python-tkinter之按钮的使用,开关方法
Jun 11 Python
Python处理session的方法整理
Aug 29 Python
Python实现快速排序的方法详解
Oct 25 Python
Python+OpenCV实现图像的全景拼接
Mar 05 Python
python 6.7 编写printTable()函数表格打印(完整代码)
Mar 25 Python
浅谈TensorFlow之稀疏张量表示
Jun 30 Python
使用Python pip怎么升级pip
Aug 11 Python
python 匿名函数与三元运算学习笔记
Oct 23 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
多数据表共用一个页的新闻发布
2006/10/09 PHP
10个值得深思的PHP面试题
2016/11/14 PHP
PHP如何获取当前主机、域名、网址、路径、端口等参数
2017/06/09 PHP
JS setCapture 区域外事件捕捉
2010/03/18 Javascript
jquery教程ajax请求json数据示例
2014/01/13 Javascript
谷歌地图打不开的解决办法
2014/08/07 Javascript
Javascript基础知识(三)BOM,DOM总结
2014/09/29 Javascript
JavaScript使用DeviceOne开发实战(一) 配置和起步
2015/12/01 Javascript
JS实现动态生成表格并提交表格数据向后端
2020/11/25 Javascript
jquery dataview数据视图插件使用方法
2016/12/23 Javascript
Bootstrap源码解读排版(1)
2016/12/23 Javascript
配置nodejs环境的方法
2017/05/13 NodeJs
微信小程序 生命周期函数详解
2017/05/24 Javascript
详解使用Vue Router导航钩子与Vuex来实现后退状态保存
2017/09/11 Javascript
js原生日历的实例(推荐)
2017/10/31 Javascript
微信小程序实现slideUp、slideDown滑动效果及点击空白隐藏功能示例
2018/12/11 Javascript
Javascript之高级数组API的使用实例
2019/03/08 Javascript
利用 Chrome Dev Tools 进行页面性能分析的步骤说明(前端性能优化)
2021/02/24 Javascript
深入浅析Python字符编码
2015/11/12 Python
Python第三方库的安装方法总结
2016/06/06 Python
Windows下的Jupyter Notebook 安装与自定义启动(图文详解)
2018/02/21 Python
python pandas中DataFrame类型数据操作函数的方法
2018/04/08 Python
python 实现在Excel末尾增加新行
2018/05/02 Python
Python 实现文件打包、上传与校验的方法
2019/02/13 Python
Python实现操纵控制windows注册表的方法分析
2019/05/24 Python
Python中if有多个条件处理方法
2020/02/26 Python
Python enumerate() 函数如何实现索引功能
2020/06/29 Python
pycharm导入源码的具体步骤
2020/08/04 Python
美国球鞋寄卖网站:Stadium Goods
2018/05/09 全球购物
Omio中国:全欧洲低价大巴、火车和航班搜索和比价
2018/08/09 全球购物
工程力学硕士生的自我评价范文
2013/11/16 职场文书
个人近期表现材料
2014/02/11 职场文书
政协工作总结2015
2015/05/20 职场文书
小王子读书笔记
2015/06/29 职场文书
新学期主题班会
2015/08/17 职场文书
Jupyter Notebook内使用argparse报错的解决方案
2021/06/03 Python