JES,嵌套的 if 语句不能正常工作
JES, nested if statement not working correctly
我一直遇到由于嵌套 If then 语句无法正常工作而导致的运行时错误。对 JES 不是很有经验,所以我要寻求帮助。基本上,(请参阅下面的源代码)我的 newX(使用 w.function randrange 来生成新的 xy 坐标)和 newY 如果超过 599 或低于 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
,它应该可以工作。
我一直遇到由于嵌套 If then 语句无法正常工作而导致的运行时错误。对 JES 不是很有经验,所以我要寻求帮助。基本上,(请参阅下面的源代码)我的 newX(使用 w.function randrange 来生成新的 xy 坐标)和 newY 如果超过 599 或低于 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
,它应该可以工作。