173 posts
Location
The hall of 1000 monkeys and only 1 typewriter
Posted 29 August 2015 - 04:15 PM
This may be considered a revive of an old topic, but it wasn't quite what I was looking for… I found a topic about a turtle repeating a specific amount of times, but it wasn't in the context that I was looking for, as I want a code to repeat 5 times. Any ideas on how to do this? If there is a post with this topic and context, please give a link?
E.G: shell.run("go down") X5
thats essentially what I want it to do. I don't want to write the same string over and over again as if it is in high quantities, it'll get pretty boring. This is for the current project and future reference
570 posts
Posted 29 August 2015 - 05:02 PM
Since you're already using the command, this should work:
shell.run("go down 5")
Though it's generally a bad idea to use shell.run for simple tasks like this, use the
turtle api instead. You can use a
for loop to repeat the action:
for i=1,5 do
turtle.down()
end
173 posts
Location
The hall of 1000 monkeys and only 1 typewriter
Posted 29 August 2015 - 06:17 PM
Ah! I see! well, i shall be using the turtle API instead from now on for tasks like this!! Thx for the help, but for future reference, what does the i=1,5 mean? ik it means to repeat x amount of times but what does the I part mean? if you could tell me that that'd be fantastic!! if not, then thx for the help anyway!!!
3057 posts
Location
United States of America
Posted 29 August 2015 - 06:37 PM
It's a numeric for loop. 'i' can be any variable you want, and will be local to the scope of the loop. It will contain the current iteration:
for i = 1, 5 do
print( "i is now: " .. i )
end
173 posts
Location
The hall of 1000 monkeys and only 1 typewriter
Posted 30 August 2015 - 06:19 PM
It's a numeric for loop. 'i' can be any variable you want, and will be local to the scope of the loop. It will contain the current iteration:
sooooooo your saying that it defines the amount of times to loop? right?
3057 posts
Location
United States of America
Posted 30 August 2015 - 07:45 PM
Sort of. i = 1 means the loop will start at 1. The second value, 5, means it will iterate until i >= 5. The lack of a third value means the loop will default to +1 increments. If you add a third value, you could have it count by different increments, including negative numbers.
Edited on 30 August 2015 - 05:46 PM
44 posts
Posted 07 September 2015 - 04:42 AM
Output:
i is now: 1
i is now: 2
i is now: 3
i is now: 4
i is now: 5
173 posts
Location
The hall of 1000 monkeys and only 1 typewriter
Posted 30 October 2015 - 03:30 PM
Sort of. i = 1 means the loop will start at 1. The second value, 5, means it will iterate until i >= 5. The lack of a third value means the loop will default to +1 increments. If you add a third value, you could have it count by different increments, including negative numbers.
ohhhh right, k, i get it now! thx :D/>