猫哥教你写爬虫 021--类与对象(上)-做业

小练习

为类calendar添加两个方法:一个删除完成项,一个添加新增项。
因为日程安排是事情和时间的配对,因此咱们用字典来存放:
{'给父母买礼物':'9:00', '学习':'10:00', '和朋友聚会':'18:30'}
接下来,须要你新建两个类方法,从而实现字典中的数据增减。
复制代码

须要的效果:

1558680284745

class calendar:
    # 日程表的日期
    date = '2020-08-08'
    # 事件清单,以字典形式给出,键为事件,值是安排的时间
    things = {'给父母买礼物':'9:00', '学习':'10:00', '和朋友聚会':'18:30'}
 @classmethod
    def thing_done(cls, thing):
        del cls.things[thing]
        print(cls.things)
 @classmethod
    def add_thing(cls, thing, time):
        cls.things[thing] = time
        print(cls.things)
while True:
    print(calendar.things)
    calendar.thing_done(input('请输入完成的事项: '))
    thing = input('请输入备忘的事件: ')
    time = input('请输入须要完成的时间: ')
    calendar.add_thing(thing,time)
    if input('是否继续添加, 回复"是"继续, 回复其余退出...') != "是":
        break
复制代码

尝试使用面向对象改写代码...

import time
import random
user1 = {
    'name': '',
    'life': 0,
    'victory':0
}
user2 = {
    'name': '',
    'life': 0,
    'victory':0
}
attack_list = [
    {
        'desc':'{} 挥剑向 {} 砍去',
        'num':20
    },
    {
        'desc':'{} 准备刚正面, 对 {} 使出一记"如来神掌"',
        'num':30
    },
    {
        'desc':'{} 撸起了袖子给 {} 一顿胖揍',
        'num':25
    },
    {
        'desc':'{} 向 {} 抛了个媚眼',
        'num':10
    },
    {
        'desc':'{} 抄起冲锋枪就是一梭子, {} 走位风骚, 轻松躲过',
        'num':0
    },
    {
        'desc':'{} 出了一个大招, {} 躲闪不及, 正中要害',
        'num':40
    }
]
user1['name'] = input('请输入玩家1的昵称: ')
user2['name'] = input('请输入玩家2的昵称: ')
while True:
    user1['victory'] = 0
    user2['victory'] = 0
    for i in range(3):
        print(' \n——————如今是第 {} 局——————'.format(i+1))
        user1['life'] = random.randint(100, 150)
        user2['life'] = random.randint(100, 150)
        print(user1['name']+'\n血量:{}'.format(user1['life']))
        print('------------------------')
        print(user2['name']+'\n血量:{}'.format(user2['life']))
        print('-----------------------')
        time.sleep(4)
        first_attack = random.randint(0,1)
        users_list = []
        if first_attack:
            user1['isfirst'] = 1
            print('漂亮!!! '+user1['name']+' 得到先发优点!')
            print('')
            users_list.append(user1)
            users_list.append(user2)
        else:
            user2['isfirst'] = 1
            print('难以置信! '+user2['name']+' 得到先发优点!')
            print('')
            users_list.append(user2)
            users_list.append(user1)
        time.sleep(2)
        while user1['life'] > 0 and user2['life'] > 0:
            tmp_rand = random.randint(0,len(attack_list)-1)
            users_list[1]['life'] = users_list[1]['life'] - attack_list[tmp_rand]['num']
            print(attack_list[tmp_rand]['desc'].format(users_list[0]['name'],users_list[1]['name'])+' 形成了{}点伤害 ==> '.format(attack_list[tmp_rand]['num'])+users_list[1]['name']+' 的剩余血量:{}'.format(users_list[1]['life']))
            time.sleep(4)
            if users_list[1]['life'] <= 0:
                print('')
                print(users_list[1]['name']+' 惨败 -_-||')
                users_list[0]['victory']+=1
                break
            tmp_rand = random.randint(0,len(attack_list)-1)
            users_list[0]['life'] = users_list[0]['life'] - attack_list[tmp_rand]['num']
            print(attack_list[tmp_rand]['desc'].format(users_list[1]['name'],users_list[0]['name'])+' 形成了{}点伤害 ==> '.format(attack_list[tmp_rand]['num'])+users_list[0]['name']+' 的剩余血量:{}'.format(users_list[0]['life']))
            time.sleep(4)
            if users_list[0]['life'] <= 0:
                print('')
                print(users_list[0]['name']+' 惨败 -_-||')
                time.sleep(3)
                users_list[1]['victory']+=1
                break
            print('-----------------------')
        if user1['victory'] == 2:
            print('')
            print('三局两胜中 '+user1['name']+' 获胜!')
            break
        if user2['victory'] == 2:
            print('')
            print('三局两胜中 '+user2['name']+' 获胜!')
            break
    print('')
    res = input('要不要再来一局? (回复"是"再来一局, 其余退出...) ') 
    if res != '是':
        break
复制代码

面向对象的写法...

import time
import random
class Player1():
    name = ''
    life = 0
    victory = 0
    skills = [
        {
            'desc': '{} 挥剑向 {} 砍去',
            'num': 20
        },
        {
            'desc': '{} 准备刚正面, 对 {} 使出一记"如来神掌"',
            'num': 30
        },
        {
            'desc': '{} 撸起了袖子给 {} 一顿胖揍',
            'num': 25
        },
        {
            'desc': '{} 向 {} 抛了个媚眼',
            'num': 10
        },
        {
            'desc': '{} 抄起冲锋枪就是一梭子, {} 走位风骚, 轻松躲过',
            'num': 0
        },
        {
            'desc': '{} 出了一个大招, {} 躲闪不及, 正中要害',
            'num': 40
        }
    ]
    is_first = False
 @classmethod
    def make_player(cls,name):
        cls.name = name
        cls.life = random.randint(100,150)
        Fight.pprint(cls.name+'\n血量:{}'.format(cls.life))
 @classmethod
    def get_first_attack(cls):
        if random.randint(0, 1):
            cls.is_first = True
            Fight.pprint('漂亮!!! {} 得到先发优点!'.format(cls.name))
 @classmethod
    def fight(cls,cls2):
        skills_index = random.randint(0,len(cls.skills)-1)
        cls2.life = cls2.life - cls.skills[skills_index]['num'] 
        Fight.pprint(cls.skills[skills_index]['desc'].format(cls.name,cls2.name)+ ' 形成了{}点伤害 ==> '.format(cls.skills[skills_index]['num'])+' {}的剩余血量:{}'.format(cls2.name,cls2.life))
class Player2():
    name = ''
    life = 0
    victory = 0
    skills = [
        {
            'desc': '{} 挥剑向 {} 砍去',
            'num': 20
        },
        {
            'desc': '{} 准备刚正面, 对 {} 使出一记"如来神掌"',
            'num': 30
        },
        {
            'desc': '{} 撸起了袖子给 {} 一顿胖揍',
            'num': 25
        },
        {
            'desc': '{} 向 {} 抛了个媚眼',
            'num': 10
        },
        {
            'desc': '{} 抄起冲锋枪就是一梭子, {} 走位风骚, 轻松躲过',
            'num': 0
        },
        {
            'desc': '{} 出了一个大招, {} 躲闪不及, 正中要害',
            'num': 40
        }
    ]
    is_first = False
 @classmethod
    def make_player(cls,name):
        cls.name = name
        cls.life = random.randint(100,150)
        Fight.pprint(cls.name+'\n血量:{}'.format(cls.life))
 @classmethod
    def get_first_attack(cls):
        if random.randint(0, 1):
            cls.is_first = True
            Fight.pprint('漂亮!!! {} 得到先发优点!'.format(cls.name))
 @classmethod
    def fight(cls,cls2):
        skills_index = random.randint(0,len(cls.skills)-1)
        cls2.life = cls2.life - cls.skills[skills_index]['num'] 
        Fight.pprint(cls.skills[skills_index]['desc'].format(cls.name,cls2.name)+ ' 形成了{}点伤害 ==> '.format(cls.skills[skills_index]['num'])+' {}的剩余血量:{}'.format(cls2.name,cls2.life))
class Fight():
    def pprint(str1):
        print(str1)
        print('-----------------')
        time.sleep(3)
    def start_fight(name1,name2):
        Player1.victory = 0
        Player2.victory = 0
        while True:
            for i in range(3):
                Fight.pprint('如今是第{}局!'.format(i+1))
                # 生成玩家
                Player1.make_player(name1)
                Player2.make_player(name2)
                # 决定首发
                Player1.get_first_attack()
                if Player1.is_first:
                    while True:
                        Player1.fight(Player2)
                        if Player2.life <= 0:
                            Fight.pprint(Player2.name + ' 惨败 -_-||')
                            Player1.victory += 1
                            break
                        Player2.fight(Player1)
                        if Player1.life <= 0:
                            Fight.pprint(Player1.name + ' 惨败 -_-||')
                            Player2.victory += 1
                            break
                else:
                    while True:
                        Player2.fight(Player1)
                        if Player1.life <= 0:
                            Fight.pprint(Player1.name + ' 惨败 -_-||')
                            Player2.victory += 1
                            break
                        Player1.fight(Player2)
                        if Player2.life <= 0:
                            Fight.pprint(Player2.name + ' 惨败 -_-||')
                            Player1.victory += 1
                            break
                if Player1.victory == 2 or Player2.victory ==2 :
                    if Player1.victory == 2:
                        Fight.pprint("{} 在三局两胜中获胜!!!".format(Player1.name))
                        break
                    else:
                        Fight.pprint("{} 在三局两胜中获胜!!!".format(Player2.name))
                        break
            if input('是否继续? 回复"是"继续, 其余退出... ') != "是":
                break
Fight.start_fight(input('请输入第一个玩家的名字: '),input('请输入第一个玩家的名字: '))
复制代码

快速跳转:

猫哥教你写爬虫 000--开篇.md
猫哥教你写爬虫 001--print()函数和变量.md
猫哥教你写爬虫 002--做业-打印皮卡丘.md
猫哥教你写爬虫 003--数据类型转换.md
猫哥教你写爬虫 004--数据类型转换-小练习.md
猫哥教你写爬虫 005--数据类型转换-小做业.md
猫哥教你写爬虫 006--条件判断和条件嵌套.md
猫哥教你写爬虫 007--条件判断和条件嵌套-小做业.md
猫哥教你写爬虫 008--input()函数.md
猫哥教你写爬虫 009--input()函数-人工智能小爱同窗.md
猫哥教你写爬虫 010--列表,字典,循环.md
猫哥教你写爬虫 011--列表,字典,循环-小做业.md
猫哥教你写爬虫 012--布尔值和四种语句.md
猫哥教你写爬虫 013--布尔值和四种语句-小做业.md
猫哥教你写爬虫 014--pk小游戏.md
猫哥教你写爬虫 015--pk小游戏(全新改版).md
猫哥教你写爬虫 016--函数.md
猫哥教你写爬虫 017--函数-小做业.md
猫哥教你写爬虫 018--debug.md
猫哥教你写爬虫 019--debug-做业.md
猫哥教你写爬虫 020--类与对象(上).md
猫哥教你写爬虫 021--类与对象(上)-做业.md
猫哥教你写爬虫 022--类与对象(下).md
猫哥教你写爬虫 023--类与对象(下)-做业.md
猫哥教你写爬虫 024--编码&&解码.md
猫哥教你写爬虫 025--编码&&解码-小做业.md
猫哥教你写爬虫 026--模块.md
猫哥教你写爬虫 027--模块介绍.md
猫哥教你写爬虫 028--模块介绍-小做业-广告牌.md
猫哥教你写爬虫 029--爬虫初探-requests.md
猫哥教你写爬虫 030--爬虫初探-requests-做业.md
猫哥教你写爬虫 031--爬虫基础-html.md
猫哥教你写爬虫 032--爬虫初体验-BeautifulSoup.md
猫哥教你写爬虫 033--爬虫初体验-BeautifulSoup-做业.md
猫哥教你写爬虫 034--爬虫-BeautifulSoup实践.md
猫哥教你写爬虫 035--爬虫-BeautifulSoup实践-做业-电影top250.md
猫哥教你写爬虫 036--爬虫-BeautifulSoup实践-做业-电影top250-做业解析.md
猫哥教你写爬虫 037--爬虫-宝宝要听歌.md
猫哥教你写爬虫 038--带参数请求.md
猫哥教你写爬虫 039--存储数据.md
猫哥教你写爬虫 040--存储数据-做业.md
猫哥教你写爬虫 041--模拟登陆-cookie.md
猫哥教你写爬虫 042--session的用法.md
猫哥教你写爬虫 043--模拟浏览器.md
猫哥教你写爬虫 044--模拟浏览器-做业.md
猫哥教你写爬虫 045--协程.md
猫哥教你写爬虫 046--协程-实践-吃什么不会胖.md
猫哥教你写爬虫 047--scrapy框架.md
猫哥教你写爬虫 048--爬虫和反爬虫.md
猫哥教你写爬虫 049--完结撒花.mdhtml

相关文章
相关标签/搜索