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

yipeiwu_com5年前Python基础

复制代码 代码如下:

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中暂存上传图片的方法

很简单的代码,记录一下。 复制代码 代码如下:     import Image     image = Image.open...

python之cv2与图像的载入、显示和保存实例

python之cv2与图像的载入、显示和保存实例

本文是OpenCV 2 Computer Vision Application Programming Cookbook读书笔记的第一篇。在笔记中将以Python语言改写每章的代码。 P...

Python基于分水岭算法解决走迷宫游戏示例

本文实例讲述了Python基于分水岭算法解决走迷宫游戏。分享给大家供大家参考,具体如下: #Solving maze with morphological transformatio...

Python中类的初始化特殊方法

什么是特殊方法?当我们在设计一个类的时候,python中有一个用于初始化的方法$__init__$,类似于java中的构造器,这个就是特殊方法,也叫作魔术方法。简单来说,特殊方法可以给你...

python 实现多线程下载m3u8格式视频并使用fmmpeg合并

python 实现多线程下载m3u8格式视频并使用fmmpeg合并

电影之类的长视频好像都用m3u8格式了,这就导致了多线程下载视频的意义不是很大,都是短视频,线不线程就没什么意义了嘛。 我们知道,m3u8的链接会下载一个文档,相当长,半小时的视频,应该...