首页 > 其他分享 >羊 老虎 饲养员 animal=random.choice([Tiger,Sheep]) 该animal类型是对象

羊 老虎 饲养员 animal=random.choice([Tiger,Sheep]) 该animal类型是对象

时间:2023-07-27 11:57:58浏览次数:39  
标签:Sheep __ room self random num animal ._

#  羊 老虎  饲养员
import random
# 基类
class Animal():
    # 属性
    def __init__(self,animal,w,call,food,room_num):
        self._animal=animal
        self._w=w
        self._call=call
        self._food=food
        self._room_num=room_num
    # 动作  吃
    def eat(self,food):
        print(f'{self._animal}吃了{food},当前的体重是{self._w}')
    # 动作 叫
    def call(self):
        print(f'{self._animal}叫了{self._call},当前的体重是{self._w}')
    def get_w(self):
        print(f'在第{self._room_num}房间,{self._animal},当前的体重是{self._w}')
        return self._w
# 继承基类
class Tiger(Animal):
    # 属性赋值
    def __init__(self,room_num):
        super().__init__('tiger',200,'wow','meat',room_num)
class Sheep(Animal):
    # 属性赋值
    def __init__(self,room_num):
        super().__init__('sheep',100,'mie','grass',room_num)
class Keeper():
    # 属性赋值
    def __init__(self):
        # 字典{房间号:动物具体的对象}
        self._rooms={}
    def put_animal(self):
        # 10个房间10个动物
        for room_num in range(1,11):

            # 动物有两种 随机放
            animal=random.choice([Tiger,Sheep])

            # 追加到字典
            self._rooms[room_num] = animal(room_num)
        return  self._rooms
    def keep(self):
        # 对10个动物喂养
        for room_num,animal in self._rooms.items():
            animal.get_w()
            animal.call()
            animal.eat(animal._food)
k=Keeper()
k.put_animal()
k.keep()

 

标签:Sheep,__,room,self,random,num,animal,._
From: https://www.cnblogs.com/haha1988/p/17584562.html

相关文章

  • 【Python】蒙特卡洛模拟 | PRNG 伪随机数发生器 | LCG 线性同余算法 | 马特赛特旋转算
    ......
  • Python随机函数random使用详解
    在python中用于生成随机数的模块是random,在使用前需要import,下面看下它的用法。random.randomrandom.random()用于生成一个0到1的随机符点数:0<=n<1.0注意: 以下代码在Python3.5下测试通过,python2版本可稍加修改描述random()方法返回随机生成的一个实数,它在(0,1)范围内。......
  • pytorch使用(四)np.random.randint用法
    np.random.randint用法np.random.randint是numpy库中用于生成随机整数的函数。它的用法如下:numpy.random.randint(low,high=None,size=None,dtype='l')其中,各个参数的含义如下:low:生成的随机整数的下限(包含)。high:生成的随机整数的上限(不包含)。如果不提供high参数,则生......
  • [ARC104E] Random LIS 题解
    [ARC104E]RandomLIS题解Link吐了,一下午就写了这一个题……主要是题解都说的很草率。然后上课的时候貌似讲的方法不是很能做(也许是我太菜了),总之我得写篇题解整理整理。首先\(n\)很小,可以直接爆搜所有相对大小,即我们去搜索\(1\)到\(n\)的排名,排名可以一样(即\(a_i\)相......
  • cpp generate uuid by random
    #include<cstdio>#include<cstdlib>#include<ctime>#include<cstdint>uint32_trand32(){return((rand()&0x3)<<30)|((rand()&0x7fff)<<15)|(rand()&0x7fff);}boolgen_uuid4(chardst[37]......
  • 题解 CF1842H【Tenzing and Random Real Numbers】
    看了题解。好难受,想用积分求概率,算了半天。发现没啥规律,不是不能算,就是太可怕了。Problem有\(n\)个\([0,1]\)范围内的均匀随机变量\(x_{1\cdotsn}\)和\(m\)条限制,每条限制形如\(x_i+x_j\le1\)或\(x_i+x_j\ge1\)。请你求出所有限制均满足的概率。对\(998244353\)......
  • LeetCode 519. Random Flip Matrix 哈希Map
    Thereisanmxnbinarygridmatrixwithallthevaluesset0initially.Designanalgorithmtorandomlypickanindex(i,j)wherematrix[i][j]==0andflipsitto1.Alltheindices(i,j)wherematrix[i][j]==0shouldbeequallylikelytobereturne......
  • Math函数之Random随机数、Date日期
    publicstaticvoidmain(String[]args)throwsParseException{Datedate1=newDate();//nowDatedate2=newDate(0);//计算机元年Datedate3=newDate(Long.MAX_VALUE);//毫秒数Datedate4=newDate(Long.MIN_VALUE);......
  • SystemVerilog Dynamic Array Randomization
    https://verificationguide.com/systemverilog/systemverilog-dynamic-array-randomization/DynamicArrayRandomizeForadynamicarray,itispossibletorandomizebotharraysizeandarrayelements.randomizedynamicarraysizeInbelowexample,dynamicarr......
  • R语言代做编程辅导ASSIGNMENT FOUR - RANDOM GRAPHS(附答案)
    全文链接:https://tecdat.cn/?p=33183PROBLEM1)CreatingRandomAdjacencyMatricesScriptName:adjMatrixInput:n...Thenumberofverticesinthegraphp...Probablitytwoverticesareconnectedplot...whetherornotthematrixshouldbeplottedasagraph......