# 目标获取10个房间里的动物 和体重 import random class Room: def __init__(self,roomnum,animal_type): # 房间号 self._roomnum=roomnum # 动物 self._animal_type=animal_type # 定义动作 def putroom(self,animal_type,roomnum): list_random_animal_type = [] # count = 0 while count < 10: num = random.randint(1, 2) list_random_animal_type.append(num) count += 1 for i in range(10): list_random_animal_type[i] = str(list_random_animal_type[i]).replace('1', 'tiger').\ replace('2', 'sheep') # print(list_random_animal_type)# 放入老虎和羊的顺序 # 将 老虎 和羊放入房间 dict1 = {} # dict1={1:1,2:2,3:1.....} for i in range(10): dict1[i] = list_random_animal_type[i] print(dict1) ''' {0: 'sheep', 1: 'tiger', 2: 'tiger', 3: 'tiger', 4: 'tiger', 5: 'sheep', 6: 'tiger', 7: 'sheep', 8: 'sheep', 9: 'sheep'} ''' class Animal: # dict2=[{'animal_type':'tiger','weight':200,'cries':'wow','food_type': 'meat'}, # {'animal_type':'sheep','weight':100,'cries':'mie','food_type': 'grass'}] # 定义动物的属性 def __init__(self,animal_type,weight,cries,food_type): self._animal_type=animal_type self._weight=weight self._cries=cries self._food_type=food_type # 定义动物的动作 长肉 # 定义动物的动作 开门 发出叫声确定动物类型 def open_door(self,weight,animal_type,cries): # 如果 叫声是wow 说明是老虎,否则是羊 if cries=='wow': animal_type='tiger' else: animal_type='sheep' print(f"投喂者敲门后,发出{self._cries}的叫声,开门的是{self._animal_type}") self._weight-= 5 # 体重下降了5 print(f'开门后,当前的{self._animal_type}的体重是{self._weight}') # # 定义动物的动作 被投喂 # def eat(self,food_type,weight): # # 如果 肉给老虎 体重加10斤;肉给羊 体重减10 # if food_type == 'meat' and animal_type == 'grass': # self._weight += 10 # print(f"{self.animal_type}吃了{food_type}后,当前的体重是{self._weight}")