gpt4 book ai didi

python - 根据属性计算类的实例

转载 作者:太空宇宙 更新时间:2023-11-03 23:56:50 25 4
gpt4 key购买 nike

计算特定属性值的实例数

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

  • creed (值可以是 "C""D"),以及
  • life (从值 100 开始,并随着程序的执行而变化)。

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

经过多次迭代,我想知道每个教派有多少只鸟“活着”。

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 次战斗,一些鸟类已经死亡。几只鸟?哪个信条?

简答

  • 单行(感谢 Carcigenicate ) dead_crows = len([bird for bird in population if bird.creed == "c" and bird.life <= 0])

  • 函数(感谢 zixuan)

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

最佳答案

您还可以在计算有多少只鸟死亡的类定义之前定义两个从 0 开始的值,例如在一只鸟“死”时添加一个 cDead += 1。 1000 次战斗后,从第一个值中减去 100,从第二个值中减去 100。然后你就会知道每个信条中有多少只鸟还活着。您还可以像这样计算每个信条有多少只鸟死了。

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 行,因为这段代码未经测试,我不知道是否有任何错误。

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

关于python - 根据属性计算类的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57379089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com