[英]Python Not Running Full Code
我嘗試通過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))
關於發生了什么的任何想法? 這項工作較早,但是現在已經壞了。 您可能會說,這個程序是一個非常非常簡單的培養皿模擬器,里面裝有一些非常不智能的單元。
獎勵問題:無限循環對您的計算機有害嗎? 我碰到了其中一些,我不想冒以任何方式,形狀或形式損壞我的機器的風險。
大多數情況下,這是因為您的算法可以生成不受您選擇的算法約束的輸入。
首先, 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,實際上是在增加對象的生命。 您可能希望以self.life的初始值飽和該值(如果您真的想康復,則為100)。
其次,即使您這樣做,也可能會遇到這樣的情況:self.attack = 20 self.defense = 30敵人。attack= 20敵人.defense = 30
從根本上說,這是一場枕頭大戰:)由於攻擊總是少於防御,因此生命永遠不會消亡,並且這種循環將永遠持續下去。 您可能要在此處引入隨機元素。
您應該更具體地說明“拒絕運行”和“超時”的含義。
我的理解是“超時”意味着您陷入了無限循環。 如果單元格之間沒有碰到一個fight()
,那么您的程序就有永遠執行的風險。
我將對該程序進行一些更改:
target()
作為普通函數移動。 這樣, Species
類將不會依賴於全局人口數組。 pprint
。 如果以上情況(隨機運行的單元格)成立,則最后的pprint將無法執行。 while
條件以len() == 1
或在最大迭代次數之后結束 HTH,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.