python:面向对象简单示例

发布于:2025-02-11 ⋅ 阅读:(57) ⋅ 点赞:(0)

编写 se2ball.py  如下

# -*- coding: utf-8 -*-
""" python 面向对象简单示例 """
import random

class Random_ball(object):
    """ 随机选双色球 """
    def __init__(self, reds=33, blues=16):
        """ 初始化 """
        self.ls = range(1,reds+1)
        self.bs = blues

    def red_ball_ls(self):
        """ 随机选红球 """
        red_ball = random.sample(self.ls, 6)
        red_ball.sort()
        return red_ball

    def blue_ball(self):
        """ 随机选蓝球 """
        return random.randint(1, self.bs)

    def random_ball_ls(self):
        """ 双色球格式化显示 """
        s = []
        for i in self.red_ball_ls():
            s.append("%02d" % i)
        return ','.join((s)) + " + %02d" % self.blue_ball()


if __name__ == '__main__':
    random_obj = Random_ball()
    print(random_obj.random_ball_ls())

运行 python se2ball.py 

02,13,18,21,23,28 + 05 


编写 big_log.py  如下

# -*- coding: utf-8 -*-
""" python 面向对象简单示例 """
import random

class Random_ball(object):
    """ 随机选大乐透 """
    def __init__(self, blues=35, yellows=12):
        """ 初始化 """
        self.bs = range(1, blues+1)
        self.ys = range(1, yellows+1)

    def blue_ball_ls(self):
        """ 随机选蓝球 """
        blue_ball = random.sample(self.bs, 5)
        blue_ball.sort()
        return blue_ball

    def yellow_ball_ls(self):
        """ 随机选黄球 """
        yellow_ball = random.sample(self.ys, 2)
        yellow_ball.sort()
        return yellow_ball

    def random_ball_bs(self):
        """ 大乐透格式化显示 """
        bl = []
        for i in self.blue_ball_ls():
            bl.append("%02d" % i)
        yl = []
        for i in self.yellow_ball_ls():
            yl.append("%02d" % i)
        return ','.join((bl)) + " + " + ','.join((yl))


if __name__ == '__main__':
    random_obj = Random_ball()
    print(random_obj.random_ball_bs())

运行 python big_lot.py 

05,10,20,31,32 + 03,05


网站公告

今日签到

点亮在社区的每一天
去签到