pythonjythonjes

JES, nested if statement not working correctly


I keep encountering a runtime error resulting from a nested If then statement not working properly. Not very experienced with JES, so thus why I am reaching out for help. Basically, (see sourcecode below) my newX (is used w. function randrange to generate new xy coords) and newY if over 599 or under, then set that var back to 0. Well, I am seeing

getPixel(picture,x,y): y (= -22) is less than 0 or bigger than the height (= 599) The error value is: Inappropriate argument value (of correct type). An error occurred attempting to pass an argument to a function. Please check line 39 of E:\dOX\HW\INF 120\e_Project5.py

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

Solution

  • I suspect the issue comes from **elif** newY > 599 or newY < 0: which means that if the previous test (newX > 599 or newX < 0) is triggered, then newY is not tested. Replace elif with ifand it should work.