我一直遇到由嵌套的 If then 语句无法正常工作而导致的运行时错误。对JES不是很有经验,所以为什么我要寻求帮助。基本上,(见下面的源代码)我的newX(用于w.函数randrange来生成新的xy坐标)和newY如果超过599或更低,则将该var设置回0。好吧,我看到了
getPixel(picture,x,y):y (= -22) 小于 0 或大于高度 (= 599) 错误值为: 不适当的参数值(类型正确)。 尝试将参数传递给函数时出错。 请检查 E:\dOX\HW\INF 120\e_Project5.py 的第 39 行
from random import *
def main( ):
#draw
pic = makeEmptyPicture(600, 600, white)
show(pic)
#for the 4 boxes
boxCount = 0
#while statement to draw
while boxCount < 4:
addRectFilled(pic, randrange(0,576), randrange(0,576), 25, 25, red)
addArcFilled(pic, randrange(0,576), randrange(0,576), 10, 10, 0, 360, blue)
boxCount = boxCount + 1
repaint(pic)
newX = 0
newY = 0
oldX = 0
oldY = 0
robotcount = 0
finished = 0
safe = 0
triggered = 0
while robotcount < 750 or finished != 1 or safe != 1 or triggered != 1:
oldX = newX
oldY = newY
newX = newX + randrange(-25, 26)
newY = newY + randrange(-25, 26)
if newX > 599 or newX < 0:
newX = 0
elif newY > 599 or newY < 0:
newY = 0
px = getPixel(pic, newX, newY)
color = getColor(px)
if color == red:
triggered = 1
printNow("trig")
elif color == blue:
safe = 1
printNow("safe")
elif robotcount == 750:
finished = 1
printNow("Fin")
addLine(pic, oldX, oldY, newX, newY, black)
robotcount = robotcount + 1
我怀疑问题来自**elif** newY > 599 or newY < 0:
这意味着如果触发了之前的测试(newX > 599 or newX < 0
),则不会测试newY
。将elif
替换为if
,它应该可以工作。