号码未添加到列表中的号码

时间:2017-04-21 03:06:43

标签: python simulator

我正在尝试制作一个进化模拟器,但我遇到了一些麻烦。我试图在列表中的数字中添加“随机变异”,但它们实际上并没有加在一起。

def Evolve(evolution, creatures):
    print(creatures)
    creatureNumber = 0
    if evolution == 0:
        print("First Generation:")
        for i in creatures:
            creatureIndex = creatureNumber + 1
            creatureNumber += 1
            print("                    Creature", creatureIndex, ":", i)
            randomMutation = random.randint(-1, 2)
            i += randomMutation
    else:
        print("Evolution", evolution, ":")
        for i in creatures:
            creatureIndex = creatureNumber + 1
            creatureNumber += 1
            print("                    Creature", creatureIndex, ":", i)
            randomMutation = random.randint(-1, 2)
            i += randomMutation
    print("")
    print("Leading Creature:", creatures.index(max(creatures))+ 1,":", max(creatures))
    EvolveQuestion(evolution, creatures)

列表“生物”在单独的函数中定义。 “i + = randomMutation”似乎只是不将数字加在一起。

2 个答案:

答案 0 :(得分:1)

我已经指出您忘记了creatureNumber + 1中的等号,因此您已使用更正后的代码更新了您的问题,但仍未获得预期结果。好。这是您的代码的下一个主要问题:

如果您有creatures = [1,1,1,1]之类的列表,并且希望将其更改为[1,2,3,4],则必须执行以下操作:

  

for i,_ in enumerate(creatures):

     creatures[i] = i+1

现在,我们回到编码。

很难从你提供的代码中猜出你想要达到的目标,但无论如何我都会试一试:

import random
creatures = [1,1,1,1]

def Evolve(evolution, creatures):
    print(creatures)
    creatureNumber = 0
    if evolution == 0:
        print("First Generation:")
        for i, item in enumerate(creatures):
            creatureIndex = creatureNumber + 1
            creatureNumber += 1
            print("                    Creature", creatureIndex, ":", creatures[i])
            randomMutation = random.randint(-1, 2)
            creatures[i] += randomMutation
    else:
        print("Evolution", evolution, ":")
        for i, item in enumerate(creatures):
            creatureIndex = creatureNumber + 1
            creatureNumber += 1
            print("                    Creature", creatureIndex, ":", creatures[i])
            randomMutation = random.randint(-1, 2)
            creatures[i] += randomMutation
    print("")
    print(creatures)    
    print("Leading Creature:", creatures.index(max(creatures))+ 1,":", max(creatures))
    print('---')
    # EvolveQuestion(evolution, creatures)

Evolve(0, creatures)
Evolve(1, creatures)
Evolve(2, creatures)
Evolve(3, creatures)
Evolve(4, creatures)
Evolve(5, creatures)

上面的代码是我对你打算实现的目标的最好猜测,但在某些方面仍然没有任何意义,但至少它显示了一些" evolution"。现在我很想知道你是否认为它对你有帮助。我会从你的评论中注意到这一点,或者如果你接受我的回答:)。

代码作为打印输出:

[1, 1, 1, 1]
First Generation:
                    Creature 1 : 1
                    Creature 2 : 1
                    Creature 3 : 1
                    Creature 4 : 1

[3, 0, 2, 3]
Leading Creature: 1 : 3
---
[3, 0, 2, 3]
Evolution 1 :
                    Creature 1 : 3
                    Creature 2 : 0
                    Creature 3 : 2
                    Creature 4 : 3

[3, 1, 4, 2]
Leading Creature: 3 : 4
---
[3, 1, 4, 2]
Evolution 2 :
                    Creature 1 : 3
                    Creature 2 : 1
                    Creature 3 : 4
                    Creature 4 : 2

[5, 3, 3, 2]
Leading Creature: 1 : 5
---
[5, 3, 3, 2]
Evolution 3 :
                    Creature 1 : 5
                    Creature 2 : 3
                    Creature 3 : 3
                    Creature 4 : 2

[6, 2, 3, 1]
Leading Creature: 1 : 6
---
[6, 2, 3, 1]
Evolution 4 :
                    Creature 1 : 6
                    Creature 2 : 2
                    Creature 3 : 3
                    Creature 4 : 1

[5, 3, 4, 0]
Leading Creature: 1 : 5
---
[5, 3, 4, 0]
Evolution 5 :
                    Creature 1 : 5
                    Creature 2 : 3
                    Creature 3 : 4
                    Creature 4 : 0

[4, 4, 6, 1]
Leading Creature: 3 : 6
---

答案 1 :(得分:1)

Look here first。您无法修改迭代变量i += randomMutation。它只会被迭代器从creatures返回的下一个元素覆盖。