error and crash of script

while doing a loop if i do radical of a negative number
it crashes the script

is there a way to only gie a warning and then continue with the script loop?

Thanks

You can test the number before:


for number in numberList:
     if number >= 0:
          sqrt_number = sqrt(number)
          # Continue on...
     else:
          # Put your warning here


Or you can do a try/except:


for number in numberList:
     try:
         sqrt_number = sqrt(number)
     except:
          # some error taking the square root, do what you need to handle this case

for the first one

it’s relitively easy to implement

but would there be sort of a mesage popup that can show and when you click it goes away
that would be more visible i guess to warn of the problem

by the way the SQRT is another function or is it builtin python ?

for the second one i don’t really understand it !

looks like a function ( try:) inside another function
so when there is an error it simply skip the line and keep going

here is the code for oe curve i did


 
 
 
 kkn=0
 a1=2
 minag=1
 maxag=360
 step1=1
 
 for n in range(minag,maxag,step1):
 
  ANGTR=n*pi/180
  ro=(((a1**2)*cos(ANGTR))/sin(ANGTR))
  
  if ro >= 0:
   ro = (ro)**0.5
          # Continue on...
  else:
   kkn+=1
          # Put your warning here
   print "   negative number "
  
 
 
 

i added a counter to show the number of problems lines
just to inform of it
but would prefer to see it in a pop up message windows

Thanks

To pop up a box, just use Draw.PupMenu


 kkn=0
 a1=2
 minag=1
 maxag=360
 step1=1
 
 for n in range(minag,maxag,step1):
     ANGTR=n*pi/180
     ro=(((a1**2)*cos(ANGTR))/sin(ANGTR))

     if ro >= 0:
          ro = (ro)**0.5
     else:
          kkn+=1
          Draw.PupMenu("Warning: Negative Number")

sqrt() is a function built into the math module that performs the same thing as n**0.5

Since the first example will work fine enough, I won’t spend too much time on the second one, but basically if there’s an error trying to take the square root (like the radicand being negative), then it’ll throw an error which is handled by the except case. For instance (not really related to this problem):


def mySqrt(n):
     try:
          return n**0.5
     except ValueError:
          print "Warning: Cannot take Square Root of Negative Number"
          return -1

a = mySqrt(4)
print a

b = mySqrt(-4)
print b