This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
jjmo18's profile picture

9x9: 2: 'for' limit must be a number

Started by jjmo18, 12 September 2013 - 03:46 PM
jjmo18 #1
Posted 12 September 2013 - 05:46 PM
title: 9x9: 2: 'for' limit must be a number
im trying to buld a house with this. im making a function to build the floor but it gives me that error. How could i do this instead?

function forward() for
  i= 0,x and x>=1 do
  turtle.forward()
  turtle.digDown()
  turtle.select(1)
  turtle.placeDown()
  end
end

function floor()
   forward()
   turtle.turnLeft()
   x= x-1
   turtle.turnLeft()
   forward()
   turtle.turnLeft()
   forward()
   turtle.turnLeft()
end

floor()
http://pastebin.com/LduN6mux
Edited by
Yevano #2
Posted 12 September 2013 - 08:58 PM
I can see what you're trying to do here, but that's not quite how for loops work. The part after the comma has to be a numerical upper limit for the variable i to reach. You're trying to give it a condition on which to stop. Since you aren't using the i variable, you might as well go for a while loop. Your loop would change to:


while x >= 1 do
	-- stuff
end


Note that you'll have to initialize x first, or else neither your condition nor your floor function will work because x doesn't exist yet. I assume this is just a snippet of code and you've done this, but just letting you know.
BigTwisty #3
Posted 13 September 2013 - 01:14 AM
No need to initialize x first. Simply use:

repeat
  -- do cool stuff
until x < 1

Even if x is local to the scope inside the loop, until will see it.
Yevano #4
Posted 13 September 2013 - 07:21 AM
No need to initialize x first. Simply use:

repeat
  -- do cool stuff
until x < 1

Even if x is local to the scope inside the loop, until will see it.

The initialization is not so it will be in scope, rather it is so the condition doesn't error given that x is nil at the time.