Python 键不';“我似乎不知道。”;计数“;内循环
嘿,这是我在这里的第二篇文章,我是python的新手,在大学学习。这是从我的第一个问题“从dict自动调用键”开始的 所以我最初在同一个for循环中设置了这个,但是,我得到了错误Python 键不';“我似乎不知道。”;计数“;内循环,python,dictionary,key,Python,Dictionary,Key,嘿,这是我在这里的第二篇文章,我是python的新手,在大学学习。这是从我的第一个问题“从dict自动调用键”开始的 所以我最初在同一个for循环中设置了这个,但是,我得到了错误 The error was:__setitem__ Attribute not found. You are trying to access a part of the object that doesn't exist. 我对dict和keys有错误的想法吗?我认为如果pictureCount=3,它将在for语
The error was:__setitem__
Attribute not found.
You are trying to access a part of the object that doesn't exist.
我对dict和keys有错误的想法吗?我认为如果pictureCount=3,它将在for语句中运行3次,并创建picture[1]、picture[2]和picture[3]变量。它似乎正在经历第一个循环,但一旦进入图片[2],就会发生错误。我的思路是,没有图片[2],只有反复创作的图片[1]。如果有任何代码,你需要让我知道
for p in range(1,pictureCount+1):
picture[p]=makePicture(pickAFile())
for p in range(1,pictureCount+1):
width[p]=getWidth(picture[p])
height[p]=getHeight(picture[p])
totalWidth=totalWidth+width[p]
height=getHeight(picture[p])
if height > totalHeight:
totalHeight=height
这是完整的代码
def comicStrip():
picture={}
width={}
height={}
totalWidth=0
totalHeight=0
pixelCount=0
loopCounter=0
pictureCount=requestInteger("How many pictures do you want in the comic strip?(1-4)")
while pictureCount <1 or pictureCount >4:
pictureCount=requestInteger("How many pictures do you want in the comic strip?(1-4)")
for p in range(1,pictureCount+1):
picture[p]=makePicture(pickAFile())
for p in range(1,pictureCount+1):
width[p]=getWidth(picture[p])
height[p]=getHeight(picture[p])
totalWidth=totalWidth+width[p]
height=getHeight(picture[p])
if height > totalHeight:
totalHeight=height
cStrip=makeEmptyPicture(totalWidth, totalHeight)
pixelCount=0
while loopCounter < pictureCount:
sourceX=0
for targetX in range(pixelCount,width[p]):
sourceY=0
for targetY in range(0,height[p]):
color = getColor(getPixel(picture[1],sourceX,sourceY))
setColor(getPixel(cStrip,targetX,targetY),color)
sourceY=sourceY+1
pixelCount=pixelCount+1
sourceX=sourceX+1
addRectFilled(cStrip,0,0,p1Width,20,white)
addRect(cStrip,0,0,p1Width,20)
addRect(cStrip,0,0,p1Width,p1Height)
caption=requestString("Enter the caption for this picture.")
addText(cStrip,1,19,str(caption))
loopCounter=loopCounter+1
def comicStrip():
图片={}
宽度={}
高度={}
总宽度=0
总高度=0
像素计数=0
循环计数器=0
pictureCount=requestInteger(“您希望漫画中有多少张图片?(1-4)”)
而pictureCount 4:
pictureCount=requestInteger(“您希望漫画中有多少张图片?(1-4)”)
对于范围内的p(1,pictureCount+1):
picture[p]=makePicture(pickAFile())
对于范围内的p(1,pictureCount+1):
宽度[p]=getWidth(图片[p])
高度[p]=getHeight(图片[p])
总宽度=总宽度+宽度[p]
高度=获取高度(图片[p])
如果高度>总高度:
总高度=高度
cStrip=makeEmptyPicture(总宽度、总高度)
像素计数=0
当loopCounter
我很确定间距是正确的,在复制和粘贴后,我必须重新调整。代码中的错误不在线
height[p]=getHeight(picture[p])
在下面两行
height=getHeight(picture[p])
该行应替换为以下内容
totalHeight = getHeight(picture[p]).
我不是python专家,但在for循环的第一次迭代之后,解释器似乎将数组(或dict)的高度变为普通变量。在第二个迭代行height[p]=getHeight(picture[p])
尝试访问元素p
,但不再存在
我尝试了下面的代码JES,得到了相同的错误
array1 ={}
array1[1] = 5
array1 = 7
array1[1] = 8
print array1[1]
在代码示例中,您是如何初始化
图片
、宽度
、高度
、总宽度
和总高度
?此外,错误消息看起来不像典型的Python错误消息,您是否正在使用某个库?更多代码会有所帮助。picture
和pictureCount
在哪里初始化?第二个循环中的宽度
和高度
。您正在使用的其他函数的实现在哪里,如makePicture
和pickAFile
?如果您能够发布尽可能完整且尽可能少的代码,这将非常有用。完整的含义:如果我们可以直接运行您提供的代码并亲自查看问题,那将是最好的。事实上,我们只能猜测程序的其他部分在做什么,以及问题可能来自哪里。最低限度的含义:尝试在没有任何不相关部分的情况下展示您的代码-尝试在向我们展示之前隔离问题。删掉一些函数调用并替换为伪值,等等。请参阅:如果您能做到这一点,您将得到更好的答案!为缺乏信息道歉。我现在知道以后的问题,如果我的代码没有它可能的那么有效,我也表示歉意,我仍在学习。当您尝试使用方括号为对象赋值时,会调用setitem。我最初的想法是,你试图把一些东西作为字典使用,而不是。