|  | 
 
 
 楼主|
发表于 2021-7-11 16:17:07
|
显示全部楼层 
| 比如集五福活动,规定单日内最高只能获取福卡10张,若想继续集五福,则需要等到次日0时0分0秒后。
 
 
 复制代码# coding:utf-8
"""
集五福,迎新春
"""
import random
from datetime import datetime
class Chances(object):
    list_chance = []
    def __init__(self):
        for i in range(0, 90):
            self.list_chance.append(0)
        for i in range(0, 5):
            self.list_chance.append(1)
        for i in range(0, 3):
            self.list_chance.append(2)
        self.list_chance.append(3)
        self.list_chance.append(4)
class Blessings(Chances):
    # now = datetime.now()
    cards = 0
    money = 0
    _blessings = 0
    happy = {
        "1": {"name": "富强福", "num": 0},
        "2": {"name": "和谐福", "num": 0},
        "3": {"name": "友善福", "num": 0},
        "4": {"name": "爱国福", "num": 0},
        "5": {"name": "敬业福", "num": 0}
    }
    def draw(self, start):
        if isinstance(start, str):
            number = random.randint(1, 5)
            for k, v in self.happy.items():
                if str(number) == k:
                    v["num"] = v["num"] + 1
                    self.cards += 1
                    print("获取到: {}".format(v["name"]))
    # 规定每天获取到的福字卡片总数不得超过十张
    def check_blessings(self):
        if self.cards > 9:
            print('=' * 20)
            raise Exception('每日最多可获取{}张福'.format(self.cards))
    def show(self):
        print("当前拥有的福:")
        for k, v in self.happy.items():
            print(v["name"], ":", v["num"])
    def service_list(self):
        service_list = {
            1: '按下<Enter>键集五福',
            2: '按下<s>键查看获取福字',
            3: '按下<c>合成五福',
            4: '按下<d>抽红包',
            5: '按下<q>退出'
        }
        for k, v in service_list.items():
            print(v)
    def cheak(self):
        for k, v in self.happy.items():
            if v['num'] > 0:
                flag = True
            else:
                flag = False
                break
        return flag
    def collect(self):
        if self.cheak():
            self._blessings += 1
            print('当前合成{}张五福'.format(self._blessings))
            for k, v in self.happy.items():
                v['num'] -= 1
        else:
            print('当前合成{}张五福'.format(self._blessings))
            for k, v in self.happy.items():
                if v['num'] == 0:
                    print('缺少{}'.format(v['name']))
            print('你的福不足!')
            print('=' * 20)
    def draw_cash(self):
        if self._blessings > 0:
            money_flag = random.choice(self.list_chance)
            if money_flag == 0:
                _money = random.randint(1, 5)
            elif money_flag == 1:
                _money = random.randint(1000, 1999)
            elif money_flag == 2:
                _money = random.randint(2000, 4999)
            elif money_flag == 3:
                _money = random.randint(5000, 9999)
            elif money_flag == 4:
                _money = random.randint(10000, 20000)
            print('抽中红包金额{}'.format(_money))
            self.money += _money
            print('红包总金额{}'.format(self.money))
            self._blessings -= 1
        else:
            print('五福不足!')
            print('=' * 20)
    def run(self):
        while True:
            self.service_list()
            start = input("请选择:")
            if start == '':
                if self.cards < 10:
                    self.draw(start)
                else:
                    try:
                        self.check_blessings()
                    except Exception as e:
                        print(e)
                print('=' * 20)
            elif start == 's':
                self.show()
                print('=' * 20)
            elif start == 'c':
                print('=' * 20)
                self.collect()
            elif start == 'q':
                print('感谢使用,再见!')
                print('=' * 20)
                break
            elif start == 'd':
                print('=' * 20)
                self.draw_cash()
            else:
                print('输入有误,重新输入!')
                print('=' * 20)
if __name__ == '__main__':
    b = Blessings()
    b.run()
    # print(b.now)
 | 
 |