gpt4 book ai didi

python - 迭代列表的字典时出现 IndexError : string index out of range,

转载 作者:太空宇宙 更新时间:2023-11-03 14:05:07 25 4
gpt4 key购买 nike

如果这应该被认为是重复的,我很抱歉,我检查了其他几个问题,但找不到错误,因为它可能对我的情况来说很特殊......

我知道问题出在这里,我用它来绘制字典中的每个字符:

编辑:好吧,这是为了抽象一个具体的例子,我不知道哪里出了问题,所以我将发布整个代码:

import pygame, random, time, sys, string, itertools

check_errors = pygame.init()

################### Main Variables ######################

PSwith = 460
PSheight = 460
playSurface = pygame.display.set_mode((PSwith, PSheight))

rowsCount = int(PSwith/10 - 1)
columnsCount = int(PSheight/10 - 1)

print('rows: ', rowsCount)
print('columns: ', columnsCount)

alphabet = list(string.ascii_letters + string.punctuation + string.digits)

red = pygame.Color(255,0,0)
blue = pygame.Color(0,0,255)
green = pygame.Color(0,255,0)
white = pygame.Color(255,255,255)
black = pygame.Color(0,0,0)

defaultColor = green
activeColor = white

fpsController = pygame.time.Clock()

################### Important Functions ######################

def drawChar(char, x, y, color):
CharFont = pygame.font.SysFont('hermit', 15)
CharSurf = CharFont.render(char, True, color)
CharRect = CharSurf.get_rect()
CharRect.midtop = (x, y)
playSurface.blit(CharSurf, CharRect)

CharPosMatrix = []
CharLibrary = []

def setCharPos(row):
for column in range(1, int(PSwith/10)):
CharPosMatrix.append((column*10, row*10))

for row in range(1, int(PSheight/10)):
setCharPos(row)

print('CharPosMatrix: ', CharPosMatrix)
print('len CharPosMatrix:', len(CharPosMatrix))


### assembling char library, with default color ###
for i in range(rowsCount * columnsCount):
CharLibrary.append([random.choice(alphabet), defaultColor])

print('CharLibrary: ', CharLibrary)
print('len CharLibrary:', len(CharLibrary))

#MatrixDict = dict(zip(CharLibrary, CharPosMatrix))
MatrixDict = dict(zip(CharPosMatrix,CharLibrary)) #zipping each character with a x,y coord
print('MatrixDict: ', MatrixDict)
print('len MatrixDict: ', len(MatrixDict))

################### Main Loop ######################

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

playSurface.fill(black)

holderlist = []
indexCounter = 0


for key, value in MatrixDict.items():
drawChar(value[0], key[0], key[1], value[1])
print(key)
print(value)

### start changing random elements ###

n = 0
for i in range(random.randint(1, 200)):
MatrixDict[random.choice(CharPosMatrix)]= random.choice(alphabet)
n += 1
print('randomly changed characters: ', n)

randColumn = 0
for i in range(10):
MatrixDict[(10, 10)] = 'X'

pygame.display.flip()

fpsController.tick(60)

我实际上知道错误产生的部分是:

        for key, value in MatrixDict.items():
drawChar(value[0], key[0], key[1], value[1])
print(key)
print(value)

如果我将 value[1] 更改为变量 green,即代码运行...

最佳答案

您一开始就正确设置了 MatrixDict,并且第一次迭代主 while 循环时一切正常。然而,在 while 循环的末尾有两行可以修改 MatrixDict,这会破坏 future 迭代的代码:

88: MatrixDict[random.choice(CharPosMatrix)] = random.choice(alphabet)
94: MatrixDict[(10, 10)] = 'X'

在这两种情况下,您都需要指定颜色和字符,例如:

88: MatrixDict[random.choice(CharPosMatrix)] = random.choice(alphabet), defaultColor
94: MatrixDict[(10, 10)] = 'X', defaultColor

关于python - 迭代列表的字典时出现 IndexError : string index out of range,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48958015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com