嘿,这是我在这里的第二篇文章,我只是陌生人,而我在Uni学习。这是我的第一个问题,即“自动从字典中调用密钥”。
所以我最初在同一个for循环中设置了这个,但是,我得到了错误
The error was:__setitem__
Attribute not found.
You are trying to access a part of the object that doesn't exist.
我对dict和键有错误的想法吗?我认为如果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
我很确定间距是正确的,我必须在复制和粘贴后重新调整。
答案 0 :(得分:0)
代码中的错误不在线
height[p]=getHeight(picture[p])
在
处有2行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]