【发布时间】:2013-07-07 11:16:38
【问题描述】:
我写错了一个程序。
def changeByThirds(pic):
w= getWidth (pic)
h = getHeight(pic)
newPic = makeEmptyPicture(w,h)
for x in range (0,w):
for y in range (0,h):
pxl = getPixel(pic, x, y)
if (y<h/3):
#some code
if (y>(h*2/3)):
#some code
else:
#some code
return (newPic)
当我执行这个程序时,第一个 if 语句 if (y<h/3): 被忽略了,所以它就像第一个 if 根本不存在一样运行。
if (y>(h*2/3)):
#some code
else:
#some code
我发现编写代码的正确方法是这样的:
def changeByThirds(pic):
w= getWidth (pic)
h = getHeight(pic)
newPic = makeEmptyPicture(w,h)
for x in range (0,w):
for y in range (0,h):
pxl = getPixel(pic, x, y)
if (y<h/3):
#some code
elif (y>(h*2/3)):
#some code
else:
#some code
return (newPic)
但是,我的问题是;
在第一个代码中 - 为什么它绕过第一个 if 语句?
【问题讨论】:
标签: python jython if-statement