Python没有运行完全Cod

2024-05-15 20:48:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着通过Spyder和一个在线IDE运行以下代码,但都没有完全完成程序。它不是超时就是拒绝运行。你知道吗

import random
from pprint import pprint

petri_dish = []

class Species:
    def __init__(self,total,name,life,attack,defense,move,location):
        area = 1000
        self.total = 100
        self.name = name
        self.life = self.total - (random.randint(1,100))
        self.attack = self.total - (random.randint(1,100))
        self.defense = self.total - (random.randint(1,100))
        self.move = self.total - (random.randint(1,100))
        self.location = [random.randint(1,area),random.randint(1,area)]
    def relocate(self):
        x_move_add = random.randint(self.location[0], self.location[0] + self.move)
        x_move_minus = random.randint(self.location[0] - self.move,self.location[0])
        y_move_add = random.randint(self.location[1], self.location[1] + self.move)
        y_move_minus = random.randint(self.location[1] - self.move,self.location[1])
        self.location = [random.randint(x_move_minus,x_move_add),random.randint(y_move_minus,y_move_add)]
        for n in range(2):
            if self.location[n] > 1000:
                self.location[n] = 1000
            elif self.location[n] < 0:
                self.location[n] = 0
    def fight(self,enemy):
        while self.life > 0 and enemy.life > 0:
            self.life = (self.life + self.defense) - enemy.attack
            enemy.life = (enemy.life + enemy.defense) - self.attack
        else:
            if self.life > enemy.life:
                print 'Species #' + str(enemy.name) + ' was eaten!'
                self.attack = self.attack + enemy.attack
                self.life = 100
                petri_dish.remove(enemy)
            else:
                print 'Species #' + str(self.name) + ' was eaten.'
                enemy.attack = enemy.attack + self.attack
                enemy.life = 100
                petri_dish.remove(self)
    def target(self):
        for z in petri_dish:
            if z.location != self.location:
                if (z.location[0] in range(self.location[0] - self.move, self.location[0] + self.move)) and (z.location[1] in range(self.location[1] - self.move, self.location[1] + self.move)):
                    self.fight(z)

for n in range(20):
    petri_dish.append(Species(0,n,0,0,0,0,0))

def show():
    for z in petri_dish:
        print z.location,z.move

def move_around():
    for x in petri_dish:
        x.relocate()
        x.target()

while len(petri_dish) > 1:
    move_around()

for x in petri_dish:
    pprint(vars(x))

你知道怎么回事吗?这个以前有用,但现在坏了。正如你可能知道的,这个程序是一个非常非常简单的培养皿模拟器,由一些非常不聪明的细胞组成。你知道吗

附加问题:无限循环对你的电脑有害吗?我已经击中其中一些,我不想冒险伤害我的机器在任何方式,形状,或形式。你知道吗


Tags: nameinselfformovedeflocationrandom
2条回答

你应该更具体一点“拒绝跑步”和“超时”是什么意思。你知道吗

我的理解是“超时”意味着你有一个无限循环。你的程序运行的风险是永远执行,如果细胞没有遇到彼此有一个fight()。你知道吗

我会对程序做一些修改:

  • target()作为带一个(或两个)参数的普通函数移动。这样,Species类就不会依赖于全局填充数组。你知道吗
  • 在每次迭代后做pprint,而不是在最后。如果上述(随机运行的单元格)保持不变,那么最后的pprint将无法执行。你知道吗
  • while条件更改为在len() == 1结束或在最大迭代次数后结束

主要是因为您的算法可以生成不受您选择的算法约束的输入。你知道吗

首先,random.randint(1,100)将产生一个介于1和100之间的数字。不过,您实际上使用的是100 - randint(1,100),它偶尔会产生0。如果您得到两个move=0的项,则两个项都不能实际移动以接合另一个项,因此循环将永远不会退出。也许只是用self.move = random.randint(1,100)来代替等等(生活和其他事情也是如此)。你知道吗

还有其他一些约束也是无效的-请看以下几行:

self.life = (self.life + self.defense) - enemy.attack
enemy.life = (enemy.life + enemy.defense) - self.attack

这有两个问题。第一,如果x.defense>;y.attack,你实际上是在给物体增加生命。你可能想把它饱和在自我生活(如果你真的想要治愈的话,也可以是100)。你知道吗

第二,即使你这么做了,你也可以有这样的案例: 自我攻击= 20 正当防卫= 30 敌人进攻= 20 敌方防御=30个

这基本上是一场枕头大战:)既然攻击总是比防御少,那么两个生命都不会真正倒下,这个循环将永远运行。你可以在这里引入一个随机元素。你知道吗

相关问题 更多 >