首页 > 解决方案 > indexError out of range 时不应该有

问题描述

我有一长行代码打印了一堆图形符号,我刚刚开始收到索引超出范围错误。它在一个函数内部,这是重要的代码:

在顶部,导入后(fr termcolor cprint,有色,时间,操作系统,随机)

rannum = random.randrange(1,20,1)

功能:

def obstacle():
    rocknum = random.randrange(0,12,1)
    rock = ["on_cyan","on_cyan","on_cyan","on_cyan","on_cyan","on_cyan","on_cyan","on_cyan","on_cyan","on_cyan","on_cyan","on_cyan"]
    rock[rocknum]= "on_cyan"
    ongroundls = [" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "] #runframe 1  list
    pos = rannum
    #print(rannum)
    if rannum == random.randrange(0,20,1):
        #print(rannum, pos)
        ongroundls[rannum] = "4"
        rock[rocknum] = "on_yellow"
    else:
        rock[rocknum] = "on_cyan"
        #print(colored(ongroundls[0],"grey","on_cyan")+colored(ongroundls[1],"grey","on_cyan")+colored(ongroundls[2],"grey","on_cyan")+colored(ongroundls[3],"grey","on_cyan")+colored(ongroundls[4],"grey","on_cyan")+colored(ongroundls[5],"grey","on_cyan")+colored(ongroundls[6],"grey","on_cyan")+colored(ongroundls[7],"grey","on_cyan")+colored(ongroundls[8],"grey","on_cyan")+colored(ongroundls[9],"grey","on_cyan")+colored(ongroundls[10],"grey","on_cyan")+colored(ongroundls[11],"grey","on_cyan")+colored(ongroundls[12],"grey","on_cyan")+colored(ongroundls[13],"grey","on_cyan")+colored(ongroundls[14],"grey","on_cyan")+colored(ongroundls[15],"grey","on_cyan")+colored(ongroundls[16],"grey","on_cyan")+colored(ongroundls[17],"grey","on_cyan")+colored(ongroundls[18],"grey","on_cyan")+colored(ongroundls[19],"grey",rock[0])+colored(ongroundls[20],"grey",rock[1])+colored(ongroundls[21],"grey",rock[2])+colored(ongroundls[22],"grey",rock[3])+colored(ongroundls[23],"grey",rock[4])+colored(ongroundls[24],"grey",rock[5])+colored(ongroundls[25],"grey",rock[6])+colored(ongroundls[26],"grey",rock[7])+colored(ongroundls[27],"grey",rock[8])+colored(ongroundls[28],"grey",rock[9])+colored(ongroundls[29],"grey",rock[10])+colored(ongroundls[30],"grey",rock[11])+colored(ongroundls[31],"grey",rock[12]))
    while pos > 19:
        ongroundls[pos] = "#"
        pos - 1
        if pos == random.randrange(0, 32, 1):
            pos == random
        print(colored(ongroundls[0],"grey","on_cyan")+colored(ongroundls[1],"grey","on_cyan")+colored(ongroundls[2],"grey","on_cyan")+colored(ongroundls[3],"grey","on_cyan")+colored(ongroundls[4],"grey","on_cyan")+colored(ongroundls[5],"grey","on_cyan")+colored(ongroundls[6],"grey","on_cyan")+colored(ongroundls[7],"grey","on_cyan")+colored(ongroundls[8],"grey","on_cyan")+colored(ongroundls[9],"grey","on_cyan")+colored(ongroundls[10],"grey","on_cyan")+colored(ongroundls[11],"grey","on_cyan")+colored(ongroundls[12],"grey","on_cyan")+colored(ongroundls[13],"grey","on_cyan")+colored(ongroundls[14],"grey","on_cyan")+colored(ongroundls[15],"grey","on_cyan")+colored(ongroundls[16],"grey","on_cyan")+colored(ongroundls[17],"grey","on_cyan")+colored(ongroundls[18],"grey","on_cyan")+colored(ongroundls[19],"grey",rock[0])+colored(ongroundls[20],"grey",rock[1])+colored(ongroundls[21],"grey",rock[2])+colored(ongroundls[22],"grey",rock[3])+colored(ongroundls[23],"grey",rock[4])+colored(ongroundls[24],"grey",rock[5])+colored(ongroundls[25],"grey",rock[6])+colored(ongroundls[26],"grey",rock[7])+colored(ongroundls[27],"grey",rock[8])+colored(ongroundls[28],"grey",rock[9])+colored(ongroundls[29],"grey",rock[10])+colored(ongroundls[30],"grey",rock[11])+colored(ongroundls[31],"grey",rock[12]))

在 while 循环中(缓冲区只是一个小的延迟和一个 os.system('cls')

    obstacle()
    buffer()
    rannum = random.randrange(1,20,1)

它工作正常,然后我做了一些小改动,我似乎无法修复它。我已经尝试更改 randrange,并且有些事情被注释掉以试图修复它。,所以这些数字不是问题开始时的数字。我能做些什么来解决它?

标签: pythonlistfunctiontermcolor

解决方案


你的问题是最后一行。rock[12]您在 的末尾硬编码print,但rock只有 12 个元素,所以最后一个有效索引是11.

正如我在评论中指出的那样,即使您解决了这个问题,代码也会被破坏;您的while循环将永远不会运行或永远不会退出,因为您永远不会pos在循环内更改(pos - 1计算新值,但从不存储它;pos -= 1也许是意图)。


推荐阅读