首页 > 其他分享 >喂 羊和老虎

喂 羊和老虎

时间:2023-07-19 18:45:18浏览次数:21  
标签:__ weight self 老虎 animal ._ def

# 导入随机数包
import random
# 第一个类 动物类
class Animal:
    # 定义属性
    def __init__(self,animal,weight,call,food):
        self._animal=animal
        self._weight=weight
        self._call=call
        self._food=food
    def call(self):
        self._weight -=5
        print(f"{self._animal}叫了一声{self._call},当前体重{self._weight}")
    def eat(self,food):
        if self._food==food:
            self._weight +=10
        else:
            self._weight -=10
        print(f"{self._animal}吃了{food},当前体重{self._weight}")
    def getWeight(self):
        print(f"{self._animal},当前体重{self._weight}")
        return self._weight
# 第二个类 老虎类
class Tiger(Animal):
    # 定义属性
    def __init__(self):
        super().__init__("老虎",200,"wow","肉")
# 第三个类  羊类
class Sheep(Animal):
    def __init__(self):
        super().__init__("羊",100,"mie","草")
# 第四个类  房间类
class Room:
    # 属性
    def __init__(self):
        # 定义一个变量 动物列表
        self._animals=[]
    def putAnimal(self,animal):
        self._animals.append(animal)
    def getAnimal(self):
        return  self._animals


# 第五个类  饲养员类
class Keep:
    # 定义属性
    def __init__(self):
        # 定义一个变量 存房间列表
        self._rooms=[]
    def putAnimalInRoom(self):
        # 10个房间里都要放动物
        for _ in range(10):
            num = random.randint(1, 2)
            if num==1:
                animal=Tiger()
            else :
                animal=Sheep()
            # 实例化房间 为了调用房间的放动物方法
            room=Room()
            room.putAnimal(animal)
            # 将房间放到房间列表里
            self._rooms.append(room)
    def keep(self):
        count=0
        # 喂动物  10个房间都需要
        for room in self._rooms:
            count+=1
            print(f"第{count}个房间")
            animals=room.getAnimal()# 从房间里获得动物列表
            for animal in animals:
                animal.getWeight()
                animal.call()
                food=animal._food
                animal.eat(food)
                animal.getWeight
# 调用饲养员
k=Keep()
k.putAnimalInRoom()
k.keep()

 

标签:__,weight,self,老虎,animal,._,def
From: https://www.cnblogs.com/haha1988/p/17566472.html

相关文章

  • 喂动物 老虎和羊
    importrandom#动物类classAnimal:def__init__(self,animal_type,weight,cries,food):self._animal_type=animal_typeself._weight=weightself._cries=criesself._food=fooddefgetWeight(self):pri......
  • 喂动物 老虎和羊
    importrandom#动物类classAnimal:def__init__(self,animal_type,weight,cries,food):self._animal_type=animal_typeself._weight=weightself._cries=criesself._food=fooddefgetWeight(self):print(f"{s......
  • FX110网:TigerWit老虎外汇口碑极差,建议投资者们远离!
    早在2021年,FX110就提醒过大家要远离TigerWit老虎外汇,FX110也陆续收到多位用户反馈,称TigerWit老虎不能出金,客服失联以及强制平仓等异常操作。然而截止到现在仍有不明真相的投资者在TigerWit老虎外汇进行投资,那到底是什么情况呢?事情经过2021年5月以来,陆续收到用户投诉:用户A 在T......
  • Python“制作”midi音乐“两只老虎”
    从网上找了很多谱子,可惜没有音乐细胞看不太懂,根据自己的理解改了好几遍,还是听不出来“两只老虎”的感觉,于是在标题上加了双引号。这样的话就只能了解本文思路了,算是抛砖引玉吧,重点是Python标准库winsound的Beep()函数可以发出37到32767赫兹之间频率的声音,其第二个参数为发声时长。f......
  • “母老虎”老师
    “母老虎”老师我们班有一个“母老虎”老师,可吓人哩!不过,有的时候,她上课却又幽默风趣,下午还和我们一起跳操呢!我们这位老师姓周,教英语,略显苍老的脸上有些皱纹,有时......
  • 森林里住着一只老虎。一天,它饿
    http://ds.163.com/article/63376ea21c09770001f18cac/http://ds.163.com/feed/63376ea21c09770001f18cac/http://ds.163.com/article/63376ea4c5e20100011e47a8/http://ds.......