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.')
        

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值