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

how do i loop this?

Started by Xares, 01 September 2012 - 12:26 PM
Xares #1
Posted 01 September 2012 - 02:26 PM
i have this digging program called "diggy3" and i want to loop it a set number of times.
I'm new to coding and i can't figure it out.
The code is in the spoiler.
Spoilerprint("How many times would you like to run the program?")
local amountOfTimes = tonumber(read())
amountOfTimes > 0 do
turtle.dig()
turtle.forward()
turtle.turnLeft()
turtle.dig()
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.digUp()
turtle.up()
turtle.dig()
turtle.turnLeft()
turtle.turnLeft()
turtle.dig()
turtle.down()
turtle.digDown()
turtle.down()
turtle.dig()
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.up()
turtle.turnLeft()
amountOfTimes = amountOfTimes - 1
if amountOfTimes == 0 then
end
if i run this then i get this error (on the screen of my turtle):

bios:206: [string "diggy3"]:3: '='
expected

plz help and thanks in advance.
KaoS #2
Posted 01 September 2012 - 02:32 PM
use a for loop,


print("How many times would you like to run the program?")
local amountOfTimes = tonumber(read())
for a=1,amountOfTimes do
turtle.dig()
turtle.forward()
turtle.turnLeft()
turtle.dig()
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.digUp()
turtle.up()
turtle.dig()
turtle.turnLeft()
turtle.turnLeft()
turtle.dig()
turtle.down()
turtle.digDown()
turtle.down()
turtle.dig()
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.up()
turtle.turnLeft()
end
BigSHinyToys #3
Posted 01 September 2012 - 02:34 PM
you were very close this should work


print("How many times would you like to run the program?")
local amountOfTimes = tonumber(read())
while amountOfTimes > 0 do
  turtle.dig()
  turtle.forward()
  turtle.turnLeft()
  turtle.dig()
  turtle.turnRight()
  turtle.turnRight()
  turtle.dig()
  turtle.digUp()
  turtle.up()
  turtle.dig()
  turtle.turnLeft()
  turtle.turnLeft()
  turtle.dig()
  turtle.down()
  turtle.digDown()
  turtle.down()
  turtle.dig()
  turtle.turnRight()
  turtle.turnRight()
  turtle.dig()
  turtle.up()
  turtle.turnLeft()
  amountOfTimes = amountOfTimes - 1
  if amountOfTimes == 0 then -- not needed
  end
end

[edit] using a for loop is better for this as stated in the post above this one.
Xares #4
Posted 01 September 2012 - 02:44 PM
thank you for your help it works now and i understand lua a bit better.
:)/>/>