hi there I have code where I have intervals.When I run the code I sometimes get this error.
" attempt to compare number with nil
stack traceback"
code is …
display.setStatusBar( display.HiddenStatusBar ) a = math.random( 60,1120) if a \> 1020 then b = math.random( 60, 800) print("B") elseif a \> 700 then b = math.random(60,480) print("BB") elseif a \> 380 then b = math.random( 60, 160) print("BBB") elseif a \< 160 then b = math.random(380, 1120) print("BBBB") end if ( a and b \> 160) and ((a \> 160) and( b \> 160)) then c = math.random( 60, 160) print("C") elseif ( a and b \< 1020) and (( a \< 1020) and ( b \< 1020) )then c = math.random(1020,1120) print("CC") elseif ( a and b \> 700 or a and b \< 800) and (( a \> 700 or a \< 800) and ( b \> 700 or b \< 800)) then c = math.random(700, 800) print("CCC") elseif( a and b \> 380 or a and b \< 480) and (( a \> 380 or a \< 480) and ( b \> 380 or b \< 480)) then c = math.random( 380, 480) print("CCCC") end print(a) print(b) print(c)
I think the composer is not getting any appropriate values to perform operations for code to print “CCC” and “CCCC”
Your code is a bit confusing, but I thing the problems are around not specifying explicitly what you are looking for in “a”.
The code “(if a or b > 160)” will translate to “if a is true or b > 160”, which is not what I think you want. Also, the statements seem redundant. “a” is not a boolean value here.
Try re-design your code because in some cases variable b or c can be nil i.e. for 380 >= a >=160 you get b = nil. Maybe try set default values for a,b and c if is it possible.
Well I changed the code a bit… This code is working but not all the time.Sometimes it shows the same error.
display.setStatusBar( display.HiddenStatusBar ) a = math.random( 60,1120) if a \> 1020 then b = math.random( 60, 800) print("B") elseif a \> 700 then b = math.random(60,480) print("BB") elseif a \> 380 then b = math.random( 60, 160) print("BBB") elseif a \< 160 then b = math.random(380, 1120) print("BBBB") end if ( a \> 160) and ( b \> 160) then c = math.random(60,160) print("C") elseif( a \> 480 and b \> 480) or ( a \< 160 and b \> 480) or ( b \< 160 and a \> 480) then c = math.random( 380, 480) print("CC") elseif ( a \< 700 and b \< 700) or ( a \< 700 and b \> 800) or ( b \< 700 and b \> 800) then c = math.random( 700,800) print("CCC") elseif( a \< 1020) and ( b \< 1020) then c = math.random(1020, 1120) print("CCCC") end print(a) print(b) print(c)
Your code is a bit confusing, but I thing the problems are around not specifying explicitly what you are looking for in “a”.
The code “(if a or b > 160)” will translate to “if a is true or b > 160”, which is not what I think you want. Also, the statements seem redundant. “a” is not a boolean value here.
Try re-design your code because in some cases variable b or c can be nil i.e. for 380 >= a >=160 you get b = nil. Maybe try set default values for a,b and c if is it possible.
Well I changed the code a bit… This code is working but not all the time.Sometimes it shows the same error.
display.setStatusBar( display.HiddenStatusBar ) a = math.random( 60,1120) if a \> 1020 then b = math.random( 60, 800) print("B") elseif a \> 700 then b = math.random(60,480) print("BB") elseif a \> 380 then b = math.random( 60, 160) print("BBB") elseif a \< 160 then b = math.random(380, 1120) print("BBBB") end if ( a \> 160) and ( b \> 160) then c = math.random(60,160) print("C") elseif( a \> 480 and b \> 480) or ( a \< 160 and b \> 480) or ( b \< 160 and a \> 480) then c = math.random( 380, 480) print("CC") elseif ( a \< 700 and b \< 700) or ( a \< 700 and b \> 800) or ( b \< 700 and b \> 800) then c = math.random( 700,800) print("CCC") elseif( a \< 1020) and ( b \< 1020) then c = math.random(1020, 1120) print("CCCC") end print(a) print(b) print(c)