class Beer(object):
def __init__(self, money, price_money, price_bottle, price_cover):
self.money = money # 钱数
self.price_money = price_money # 每瓶花的钱
self.price_bottle = price_bottle # 一瓶值的空瓶数
self.price_cover = price_cover # 一瓶值的瓶盖数
self.beer = self.money // self.price_money
self.bottle = self.beer # 瓶子
self.cover = self.bottle # 盖子
def __repr__(self):
return f'喝酒数:{self.beer}, 剩下空瓶数:{self.bottle}, 剩下瓶盖数:{self.cover}'
def bottle_to_beer(self):
while self.bottle >= self.price_bottle:
add = self.bottle // self.price_bottle
self.beer += add
self.bottle += add
self.cover += add
self.bottle -= self.price_bottle * add
def cover_to_beer(self):
while self.cover >= self.price_cover:
add = self.cover // self.price_cover
self.beer += add
self.bottle += add
self.cover += add
self.cover -= self.price_cover * add
if __name__ == '__main__':
beer = Beer(money=10, price_money=2, price_bottle=2, price_cover=4)
while beer.bottle >= 2 or beer.cover >= 4:
beer.bottle_to_beer()
beer.cover_to_beer()
print(beer)