Jump to content




Basic Straightline Turtle Loop


  • You cannot reply to this topic
4 replies to this topic

#1 Robinlemon

  • Members
  • 42 posts

Posted 05 April 2014 - 08:02 AM

So basicaly im making a turtle that asks me how many block do i want to go and if i put 20 it will go forward 20 blocks and place blocks underneath it as it goes, pretty basic right!

I started using a for loop with variable but that dint work so i tried a while loop and it seems that hasn't worked either so now its down to those ninja coding experts to try and help me!

:11:end expected (to close if at line 9)

Any help Much Appreciated!

print("Robin's Straight Line Program V1.0")
print("How long do you want your line to be?")
x = read()

while true do
   turtle.forward()
   turtle.placeDown()
   x = x-1
   if (x == 0) then
	   break
	   print("Finished!")
end

Edited by Robinlemon, 05 April 2014 - 08:09 AM.


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 05 April 2014 - 08:41 AM

read() returns a string ("text") value, not a number. You can convert it with tonumber(), eg:

x = tonumber( read() )

Note that tonumber() returns nil if it can't do the conversion (eg you pass it a string along the lines of "cabbage").

Here's a couple of ways this could be done:

print("Robin's Straight Line Program V1.0")
print("How long do you want your line to be?")
x = tonumber( read() )

while true do
   turtle.forward()
   turtle.placeDown()
   x = x - 1
   if (x == 0) then
           break
           print("Finished!")
   end  -- You were missing this "end"!
end

print("Robin's Straight Line Program V1.0")
print("How long do you want your line to be?")

local x
while not x do   -- A variable with no value counts as "false", whereas a variable with a value counts as "true".
   x = tonumber( read() )
end

for i = 1,x do
   turtle.forward()
   turtle.placeDown()
end

print("Finished!")


#3 johnneijzen

  • Members
  • 40 posts
  • LocationPhilippines

Posted 05 April 2014 - 08:57 AM

here my way so doing this

Local Dist = 0

print("Robin's Straight Line Program V1.0")
print("How long do you want your line to be?")
input = io.read()
Dist = tonumber(input)
for i = 1, dist do
   turtle.forward()
   turtle.placeDown()
end


But you want it to dig and place here

Local Dist = 0

print("Robin's Straight Line Program V1.0")
print("How long do you want your line to be?")
input = io.read()
Dist = tonumber(input)
for i = 1, dist do
  if turtle.detect() then
    turtle.dig()
  end
  turtle.forward()
  turtle.select(1)
  turtle.placeDown()
end
 

Edited by johnneijzen, 05 April 2014 - 08:59 AM.


#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 05 April 2014 - 08:59 AM

View Postjohnneijzen, on 05 April 2014 - 08:57 AM, said:

here my way so doing this

Spoiler

But you want it to dig and place here

Spoiler
use [code][/code] tags... also you have errors in your code, our purpose here in Ask a Pro is to solve problems, not create more.

#5 johnneijzen

  • Members
  • 40 posts
  • LocationPhilippines

Posted 05 April 2014 - 10:46 AM

sorry





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users