栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python实现电梯

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python实现电梯

写一个Building类,并实现电梯功能

Building有不同的入口,我们假设不同入口进入的单元楼是不同的,但都属于同一单元

实现乘电梯上楼与离开楼的功能,并在数据错误时进行报错

代码如下:

class BuildingError(Exception):
    pass


class Building:
    number_created = 0
    def __init__(self, height, entries):
        Building.number_created += 1
        self.height = height
        self.entries = entries
        self.the_entries = entries.split()
        self.lifts = {entry: 0 for entry in self.the_entries}
        self.occupancies = {(entry, floor): 0
                                for entry in self.the_entries
                                    for floor in range(0, self.height)
                           }
        
    def __repr__(self):
        return f"Building({self.height}, '{self.entries}')"
    
    def __str__(self):
        return 'Building with ' +
               (self.height == 0 and '1 floor ' or
                f'{self.height + 1} floors '
               ) + 'accessible from entries: ' + ', '.join(self.the_entries)
    
    def go_to_floor_from_entry(self, floor, entry, nb_of_people):
        if not 0 <= floor <= self.height
           or entry not in self.the_entries
           or nb_of_people <= 0:
            raise BuildingError("That makes no sense!")
        self.occupancies[entry, floor] += nb_of_people
        if floor:
            if self.lifts[entry]:
                print('Wait, lift has to go down', self.lifts[entry],
                      self.lifts[entry] > 1 and 'floors...'
                      or 'floor...'
                     )
            self.lifts[entry] = floor

    def leave_floor_from_entry(self, floor, entry, nb_of_people):
        if not 0 <= floor <= self.height
           or entry not in self.the_entries
           or nb_of_people <= 0:
            raise BuildingError("That makes no sense!")
        if self.occupancies[entry, floor] < nb_of_people:
            raise BuildingError("There aren't that many people on that floor!")
        self.occupancies[entry, floor] -= nb_of_people
        if floor:
            if self.lifts[entry] > floor:
                difference = self.lifts[entry] - floor
                print('Wait, lift has to go down', difference,
                      difference != 1 and 'floors...' or 'floor...'
                     )
            if self.lifts[entry] < floor:
                difference = floor - self.lifts[entry]
                print('Wait, lift has to go up', difference,
                      difference != 1 and 'floors...' or 'floor...'
                     )
            self.lifts[entry] = 0

    
def compare_occupancies(building_1, building_2):
    occupancy_1 = sum(building_1.occupancies.values())
    occupancy_2 = sum(building_2.occupancies.values())
    if occupancy_1 == occupancy_2:
        print('There is the same number of occupants in both buildings.')
    elif occupancy_1 > occupancy_2:
        print('There are more occupants in the first building.')
    else:
        print('There are more occupants in the second building.')
        

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/918692.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号