简体   繁体   English

如何修复python中的“IndexError:列表索引超出范围”?

[英]How to fix 'IndexError: list index out of range' in python?

I am in year 10 and really bad at coding.我在读 10 年级,非常不擅长编码。 Basically I am trying to make a really simple evolution simulator and it has been working well so far but I ran into this error when using lists to define variables.基本上我正在尝试制作一个非常简单的进化模拟器,到目前为止它一直运行良好,但是在使用列表定义变量时遇到了这个错误。 I attached the full code.我附上了完整的代码。

#************************#
#*  Jadday's Amazing    *#
#* Evolution Simulator  *#
#************************#

import random

print("The Evolution Starts Here")

numcs = 100

def generation1():
    global numcs
    profiles = []
    maxscore = 0
    minscore = 100
    x = 0
    for i in range(0,numcs):
        a = random.randint(0,75)
        b = random.randint(a,100)
        score = random.randint(a,b)
        name = [a,b]
        x = str(x)
        x = int(x)
        x = x + 1

        if score > maxscore:
            maxscore = score
            score = str(score)
            best = score
            score = int(score)

        if score < minscore:
            minscore = score
            score = str(score)
            worst = score
            score = int(score)

        score = str(score)
        profile = score
        score = int(score)
        profiles.append(score)
        profiles = [int(x) for x in profiles]
        profiles.sort()
        print(profile)
    print("")
    print("")
    print("The best was:")
    print(best)
    print("")
    print("The worst was:")
    print(worst)

    numcs = numcs // 2

    templength = len(profiles)
    length = templength // 2

    for x in range(0,length):
        del profiles[0]

    print("")
    print(profiles)



def generation():
    global numcs
    profiles = []
    maxscore = 0
    minscore = 100
    x = 0
    for i in range(0,numcs):
        a = profiles[i]
        a = a / 2
        b = profiles[i]
        b = b * 2
        score = random.randint(a,b)
        name = [a,b]
        x = str(x)
        x = int(x)
        x = x + 1

        if score > maxscore:
            maxscore = score
            score = str(score)
            best = score
            score = int(score)

        if score < minscore:
            minscore = score
            score = str(score)
            worst = score
            score = int(score)

        score = str(score)
        profile = score
        score = int(score)
        profiles.append(score)
        profiles = [int(x) for x in profiles]
        profiles.sort()
        print(profile)
    print("")
    print("")
    print("The best was:")
    print(best)
    print("")
    print("The worst was:")
    print(worst)

    numcs = numcs // 2

    templength = len(profiles)
    length = templength // 2

    for x in range(0,length):
        del profiles[0]

    print("")
    print(profiles)




generation1()
for x in range(0,6):
    generation()
    numcs = numcs // 2

The error is in the line 'a = profiles[i]' when defining 'generation'.定义“生成”时,错误位于“a = 配置文件[i]”行中。 I most likely am doing the simulator really inefficiently but I am trying to make it simpler.我很可能在做模拟器时效率很低,但我试图让它更简单。 If anyone knows how to solve this I would appreciate it.如果有人知道如何解决这个问题,我将不胜感激。 Thanks谢谢

In function generation1 your profiles = [] is empty list.在函数generation1您的profiles = []是空列表。 In the following line, you try to take profiles[i] where i ranges from 0 to 100.在下面的行中,您尝试使用profiles[i] ,其中i范围从 0 到 100。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM