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 相关文章推荐
tensorflow学习笔记之mnist的卷积神经网络实例
Apr 15 Python
Python实现爬虫设置代理IP和伪装成浏览器的方法分享
May 07 Python
使用Python的Django和layim实现即时通讯的方法
May 25 Python
Python带动态参数功能的sqlite工具类
May 26 Python
详解flask表单提交的两种方式
Jul 21 Python
python获取Linux发行版名称
Aug 30 Python
IronPython连接MySQL的方法步骤
Dec 27 Python
浅谈pytorch torch.backends.cudnn设置作用
Feb 20 Python
使用Python第三方库pygame写个贪吃蛇小游戏
Mar 06 Python
python获取系统内存占用信息的实例方法
Jul 17 Python
如何用python免费看美剧
Aug 11 Python
彻底弄懂Python中的回调函数(callback)
Jun 25 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
用PHP制作的意见反馈表源码
2007/03/11 PHP
php遍历数组的方法分享
2012/03/22 PHP
PHP+jQuery+Ajax实现分页效果 jPaginate插件的应用
2015/10/09 PHP
PHP实现微信红包金额拆分试玩的算法示例
2018/04/07 PHP
用JavaScript事件串连执行多个处理过程的方法
2007/03/09 Javascript
jquery异步调用页面后台方法‏(asp.net)
2011/03/01 Javascript
jQuery层次选择器选择元素使用介绍
2013/04/18 Javascript
谷歌浏览器不支持showModalDialog模态对话框的解决方法
2014/09/22 Javascript
原生js配合cookie制作保存路径的拖拽
2015/12/29 Javascript
jQuery CSS3自定义美化Checkbox实现代码
2016/05/12 Javascript
简单实现jQuery进度条轮播实例代码
2016/06/20 Javascript
用jQuery的AJax实现异步访问、异步加载
2016/11/02 Javascript
简单实现jQuery多选框功能
2017/01/09 Javascript
JS 组件系列之Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案
2017/06/30 Javascript
纯JavaScript实现实时反馈系统时间
2017/10/26 Javascript
QQ跳转支付宝并自动领红包脚本(最新)
2018/06/22 Javascript
微信小程序实现红包雨功能
2018/07/11 Javascript
jquery的$().each和$.each的区别
2019/01/18 jQuery
[52:06]FNATIC vs NIP 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/19 DOTA
深入理解Python 代码优化详解
2014/10/27 Python
Python之自动获取公网IP的实例讲解
2017/10/01 Python
Django权限机制实现代码详解
2018/02/05 Python
matplotlib给子图添加图例的方法
2018/08/03 Python
python [:3] 实现提取数组中的数
2019/11/27 Python
NumPy排序的实现
2020/01/21 Python
Python基于smtplib协议实现发送邮件
2020/06/03 Python
python+playwright微软自动化工具的使用
2021/02/02 Python
英国门把手公司:Door Handle Company
2019/05/12 全球购物
哈理工毕业生的求职信
2013/12/22 职场文书
自我评价的写作规则
2014/01/06 职场文书
致100米运动员广播稿
2014/02/14 职场文书
小学兴趣小组活动总结
2014/07/07 职场文书
工作迟到检讨书范文
2015/05/06 职场文书
岗位聘任协议书
2015/09/21 职场文书
一文带你探究MySQL中的NULL
2021/11/11 MySQL