python - 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,并且有些事情被注释掉以试图修复它。,所以这些数字不是问题开始时的数字。我能做些什么来解决它?
解决方案
你的问题是最后一行。rock[12]
您在 的末尾硬编码print
,但rock
只有 12 个元素,所以最后一个有效索引是11
.
正如我在评论中指出的那样,即使您解决了这个问题,代码也会被破坏;您的while
循环将永远不会运行或永远不会退出,因为您永远不会pos
在循环内更改(pos - 1
计算新值,但从不存储它;pos -= 1
也许是意图)。
推荐阅读
- php - 如果某些值未填写在表单中,则不会更新数据库
- java - 安装 Jython 以使用 PythonInterpreter
- android-studio - 当我尝试安装 Android Studio Emulator 时。我得到错误。我该如何解决这个问题
- python - 如何从使用 for 循环创建的嵌套列表中删除空格?
- regex - 为什么 VSCode 中的 `f(?=\=)` 正则表达式无效?
- javascript - 如何使用 rxjs 和 redux observable 实现 Promise 类型的场景?
- android - 为什么我在 android react native 的 Restful api 的 GET 请求中得到 html 响应作为数据?
- eclipse - 自动导出 eclipse python (pydev) 项目
- c# - 试图在统一 c# 中保存 xml 文件但得到一个空文件
- blazor-server-side - 在 Blazor ServerSide 中挂钩 SignalR 事件