Jump to content




turtle:10: Expected number


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

#1 Hexus_One

  • New Members
  • 9 posts

Posted 18 October 2012 - 08:33 AM

So, I've typed up the following code (automatically digs a stripmine and places torches)
function Forward(x)
 local a=0
 repeat
  if turtle.forward() then
   a=a+1
  else
   print("Obstruction")
   wait(0.5)
  end
 until a==x
end

function Backward(x)
 local a=0
 repeat
  if turtle.backward() then
   a=a+1
  else
   print("Obstruction")
   wait(0.5)
  end
 until a==x
end

function Left(x)
 for _=1,x do
  turtle.turnLeft()
 end
end

function Mine(x)
 local a=0
 repeat
  turtle.dig()
  if turtle.forward() then
   turtle.digDown()
   a=a+1
  else
   print("Obstruction")
   wait(0.5)
  end
 until a==x
end

local tArgs={ ... }
if #tArgs~=1 then
 write("Place torches in top left slot.nUsage: stripmine <branches>n")
 return
end

if tonumber(tArgs[1])<1 then
 write("Must have one or more branches!n")
 return
end

turtle.select(1)
for _=1,tArgs[1] do
 Mine(2)
 turtle.placeDown()
 Left(1)
 Mine(6)
 Left(2)
 Forward(6)
 Mine(6)
 Backward(6)
 Left(1)
end
print("Finished!")
And for some reason, my turtle keeps throwing "turtle:10: Expected number" at me. There's probably something with the turtle API, and the line of code in the API is :
  local id = native[s_command]( ... )
So, what's wrong?

#2 Tiin57

    Java Lunatic

  • Members
  • 1,412 posts
  • LocationIndiana, United States

Posted 18 October 2012 - 08:35 AM

Are you passing x as a number?

#3 Hexus_One

  • New Members
  • 9 posts

Posted 18 October 2012 - 08:37 AM

Spotted missing capital letters in digDown and turnLeft. I'll see if that fixes it.

#4 Hexus_One

  • New Members
  • 9 posts

Posted 18 October 2012 - 08:46 AM

View Posttiin57, on 18 October 2012 - 08:35 AM, said:

Are you passing x as a number?
It should be. Not sure how it isn't working.

#5 JoshhT

  • New Members
  • 64 posts
  • LocationAustralia

Posted 18 October 2012 - 12:22 PM

Use this instead.

until a == tonumber(x)


#6 Doyle3694

  • Members
  • 815 posts

Posted 18 October 2012 - 12:23 PM

why tonumber a number?

#7 JoshhT

  • New Members
  • 64 posts
  • LocationAustralia

Posted 18 October 2012 - 12:32 PM

View PostDoyle3694, on 18 October 2012 - 12:23 PM, said:

why tonumber a number?

Topic title : Expected number.
Error @ line 10 : until a == x

It must be passing x as a string and not a number.
I had this issue a few days ago.

Edit: It IS passing x as a string. I just made a test file with the following.

args = {...}

if args[1] == true then
  print("True")
elseif args[1] == false then
  print("False")
elseif args[1] == 1 then
  print("One")
elseif tonumber(args[1]) == 2 then
  print("Two")
else
  print("Invalid")
end

Go on, run it. Everything you type will return invalid unless you type 2.

Edited by JoshhT, 18 October 2012 - 12:39 PM.


#8 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 18 October 2012 - 12:41 PM

Yeah tonumber() should work

#9 Hexus_One

  • New Members
  • 9 posts

Posted 20 October 2012 - 05:13 AM

View PostJoshhT, on 18 October 2012 - 12:32 PM, said:

View PostDoyle3694, on 18 October 2012 - 12:23 PM, said:

why tonumber a number?

Topic title : Expected number.
Error @ line 10 : until a == x

It must be passing x as a string and not a number.
I had this issue a few days ago.

Edit: It IS passing x as a string. I just made a test file with the following.

args = {...}

if args[1] == true then
  print("True")
elseif args[1] == false then
  print("False")
elseif args[1] == 1 then
  print("One")
elseif tonumber(args[1]) == 2 then
  print("Two")
else
  print("Invalid")
end

Go on, run it. Everything you type will return invalid unless you type 2.
I've managed to fix it WITHOUT having to use tonumber().
Turns out, I was using wait() instead of sleep().
function Forward(x)
local a=0
repeat
  if turtle.forward() then
   a=a+1
  else
   print("Obstruction")
  sleep(0.5)
  end
until a==x
end
function Backward(x)
local a=0
repeat
  if turtle.back() then
   a=a+1
  else
   print("Obstruction")
   sleep(0.5)
  end
until a==x
end
function Left(x)
for _=1,x do
  turtle.turnLeft()
end
end
function Mine(x)
local a=0
repeat
  turtle.dig()
  if turtle.forward() then
   turtle.digDown()
   a=a+1
  else
   print("Obstruction")
   sleep(0.5)
  end
until a==x
end
local tArgs={ ... }
if #tArgs~=1 then
write("Place torches in top left slot.nUsage: stripmine <branches>n")
return
end
if tonumber(tArgs[1])<1 then
write("Must have one or more branches!n")
return
end
turtle.select(1)
for _=1,tArgs[1] do
Mine(3)
turtle.placeDown()
Left(1)
Mine(6)
Left(2)
Forward(6)
Mine(6)
Backward(6)
Left(1)
end
print("Finished!")


#10 Hexus_One

  • New Members
  • 9 posts

Posted 20 October 2012 - 05:15 AM

By the way, the program itself was called "stripmine", not "turtle".

#11 ChunLing

  • Members
  • 2,027 posts

Posted 20 October 2012 - 07:51 AM

Okay, that needs to be in the "get help faster" section: Tell us the error line in your program rather than telling us to hunt down an error in the existing API. Yes, it is possible that there are errors in the existing API, but if so that's something to report in the Bugs forum.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users