基于属性计算类的实例 [英] counting instances of a class based on attributes

查看:61
本文介绍了基于属性计算类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类,名为:bird.该类的每个实例都有两个属性:

I have created a class, named: bird. Each instance of that class has two attributes:

  • creed(其值可以是 "C""D"),以及
  • life(从 100 开始,随着程序的执行而变化).
  • creed (which can be either "C" or "D" in value), and
  • life (which starts with a value of 100, and changes as the program executes).

程序模拟两只活鸟的随机相遇,鸟在每次相遇后改变life的值.一旦达到 0,这只鸟就会被排除在进一步的交互之外(即死亡).

The program simulates random encounters of two live birds, the birds change the value of life after each encounter. Once reaching 0, the bird is excluded from further interactions (i.e. dead).

经过多次迭代,我想知道每个信条的活"鸟有多少.

After several iterations, I wish to know how many birds of each creed are "alive".

import random


class Bird:
    Doves = 100
    Crows = 100

    # Initializer / Instance Attributes
    def __init__(self, creed, life):
        self.creed = creed
        self.life = life


def slct_parties():
    first = random.randint(0, (Bird.Crows + Bird.Doves -1))
    second = random.randint(0, (Bird.Crows + Bird.Doves -1))
    while first == second or population[first].life < 1 or population[second].life < 1:
        first = random.randint(0, (Bird.Crows + Bird.Doves - 1))
        second = random.randint(0, (Bird.Crows + Bird.Doves - 1))
    return first, second


#   initiating Population
population = []

for bird in range(0, Bird.Crows):
    population.append(Bird('C', 100))

for bird in range(0, Bird.Doves):
    population.append(Bird('D', 100))

for x in range(1, 1000):
    Contest = slct_parties()
    l1 = population[Contest[0]].life
    l2 = population[Contest[1]].life
    # battle
    if population[Contest[0]].creed != population[Contest[0]].creed:
        if population[Contest[0]].creed == 'D':
            population[Contest[0]].life += 1
            population[Contest[1]].life += 1
        else:
            population[Contest[0]].life += -5
            population[Contest[1]].life += -10
    elif population[Contest[0]].creed == 'C':
        population[Contest[0]].life += 5
        population[Contest[1]].life += -20
    else:
        population[Contest[0]].life += -20
        population[Contest[1]].life += 5

    print("The battle was between {} number {} with {} life, and {} number {} with {} life"
        .format(population[Contest[0]].creed, Contest[0], population[Contest[0]].life, population[Contest[1]].creed,
        Contest[1], population[Contest[1]].life))

经过 1,000 场战斗,有些鸟已经死亡.几只鸟?哪个信条?

After 1,000 battles, some birds have died. How many birds? Which creed?

  • 单班轮(感谢 Carcigenicate)dead_crows = len([如果bird.creed == "c" 和bird.life <= 0])

函数(感谢紫轩)

def DeadConter(crd):
    dead = 0
    for bird in population:
        if bird.life <= 0 and bird.creed == crd:
            dead += 1
        else:
            pass
    return dead

推荐答案

您还可以定义 2 个值,它们都在定义类的定义之前从 0 开始,以计算死亡的鸟类数量,例如添加一个 cDead += 1 当一只鸟死了".1000 场战斗后,从第一个值中减去 100,从第二个值中减去 100.然后你会得到,每个信条有多少只活鸟.您还可以像这样计算每个信条的死鸟数量.

You can also define 2 values that both start at 0 before the definition of the class that count how many birds are dead, like adding one cDead += 1 when a bird becomes "dead". After the 1000 battles, subtract 100 from the first value and subtract 100 from the second value. You get then, how many bird of each creed are alive. You can also count how many birds of each creed are dead, like this.

import random

c_dead = 0
d_dead = 0

class Bird:
    Doves = 100
    Crows = 100

    # Initializer / Instance Attributes
    def __init__(self, creed, life):
        self.creed = creed
        self.life = life


def slct_parties():
    first = random.randint(0, (Bird.Crows + Bird.Doves -1))
    second = random.randint(0, (Bird.Crows + Bird.Doves -1))
    while first == second or population[first].life < 1 or population[second].life < 1:
        first = random.randint(0, (Bird.Crows + Bird.Doves - 1))
        second = random.randint(0, (Bird.Crows + Bird.Doves - 1))
    return first, second


#   initiating Population
population = []

for bird in range(0, Bird.Crows):
    population.append(Bird('C', 100))

for bird in range(0, Bird.Doves):
    population.append(Bird('D', 100))

for x in range(1, 1000):
    Contest = slct_parties()
    l1 = population[Contest[0]].life
    l2 = population[Contest[1]].life
    # battle
    if population[Contest[0]].creed != population[Contest[0]].creed:
        if population[Contest[0]].creed == 'D':
            population[Contest[0]].life += 1
            population[Contest[1]].life += 1
        else:
            population[Contest[0]].life += -5
            population[Contest[1]].life += -10
    elif population[Contest[0]].creed == 'C':
        population[Contest[0]].life += 5
        population[Contest[1]].life += -20
    else:
        population[Contest[0]].life += -20
        population[Contest[1]].life += 5

for bird in population:
    if bird.life <= 0:
        if bird.creed == "C":
            c_dead += 1
        elif bird.creed == "D":
            d_dead += 1
        else:
            print("Dead birds failed to count") # should never happen, this is a good check for bugs

print("The battle was between {} number {} with {} life, and {} number {} with {} life"
        .format(population[Contest[0]].creed, Contest[0], population[Contest[0]].life, population[Contest[1]].creed,
        Contest[1], population[Contest[1]].life))
#print dead birds here
print("Number of alive birds")
print(str(100-c_dead) + ": Doves" + " " + str(100-d_dead) + ": Crows")

我加了else行,因为这段代码未经测试,不知道有没有bug.

I added the else line because this code is untested, I don't know if there are any bugs.

编辑:代码现在已经过测试,我已经更改了几行以使其正常工作.

EDIT: The code has been tested now and I've changed a few lines to get it working.

这篇关于基于属性计算类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆