7 posts
Posted 15 July 2014 - 10:33 PM
Hey everybody,
I'm just beginning programmation and I have some problems.
I have a problem on this section of my program and I can't resolve it.
Thank you for your help!!
(sorry for misspelling, I don"t speak very good english)
for i= 1,16 do
seeds = turtle.getItemCount(turtle.select(i))
NBRseeds = seeds + NBRseeds
end
Edited on 15 July 2014 - 09:01 PM
3790 posts
Location
Lincoln, Nebraska
Posted 15 July 2014 - 11:02 PM
Fixed the code formatting for you. You can put code in
tags, and it'll look nicer that way.
1852 posts
Location
Sweden
Posted 15 July 2014 - 11:12 PM
Hey, welcome to the forums. Before I help you with the code I'll give you some tips when asking questions here
Now onto the problem, the argument you're putting into
turtle.getItemCount should in this case be
i.What you're doing wrong is you're trying to put a function as an argument, it would be okay if the function would return a number, but in this case it returns a boolean( as you can see on the
wiki )
So here's how the code should look like
for i = 1, 16 do
seeds = turtle.getItemCount( i ) -- removed turtle.select
NBRseeds = seeds + NBRseeds
end
And if you're just beginning programming then I suggest you check out the
PIL and the
wiki
7 posts
Posted 15 July 2014 - 11:14 PM
Oups. Thank you!!!
656 posts
Posted 15 July 2014 - 11:16 PM
First, please post the full error, with line number and full code. This time the issue was easy to find, but it can be a pain when people don't post all details.
The syntax for turtle.getItemCount is as follows:
local count = turtle.getItemCount(<slotNumber>)
You're passing it the return value of turtle.select, which makes no sense. I suggest changing your program to this:
local total
for i = 1, 16 do
total = total + turtle.getItemCount(i)
end
EDIT: Ninja'd
Edited on 15 July 2014 - 09:20 PM
7 posts
Posted 15 July 2014 - 11:39 PM
Sorry for my mistake!!
A big thank you to everybody who help me!!
7 posts
Posted 16 July 2014 - 12:15 AM
Sorry. The problem isn't resolve.
(the program is not finish.)
Problem:
Error : attempt to perform arithmetic__add on number and nil
Line: 8 to 10
local turn = 1
local NBRseeds
function firstTime()
print("needed "..area.." seeds")
os.sleep(10)
for i= 1,16 do -- the problem is here!
NBRseeds = turtle.getItemCount(i) + NBRseeds -- the problem is here!
end -- the problem is here!
if NBRseeds >= area then
print("number reaches")
constructionOfTheFarm()
else
missings = area - NBRseeds
print("missing"..missings.."seeds")
NBRseeds = 0
firstTime()
end
end
function constructionOfTheFarm()
for i = 1, x do
for o = 1, z do
turtle.DigDown()
slot=1
while not turtle.placeDown() do
turtle.select(slot)
slot=slot+1
if slot>16 then
break
end
turtle.placeDown()
turtle.forward()
end
if turn == 1 then
turn = 0
else
turn = 1
end
if turn == 1 then
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
else
turtle.turnRight()
turtle.forward()
turtle.turnRight()
end
end
end
end
-- information phase
print("Welcome to the turtle's farm")
print(" ")
print("Information needed")
print(" ")
term.write("First Time?(Yes/No)")-- by default: No
firstTimes = io.read()
term.write("mode 9x9 activated?(Yes/No)") -- by default: Yes / recommanded
mode9x9 = io.read()
term.write("Length:") -- by default: 9
x = math.abs(tonumber(io.read()))
term.write("Width:") -- by default: 9
z = math.abs(tonumber(io.read()))
if mode9x9 == "Yes" then
x=x*9
z=z*9
else
x = x
z = z
end
if not x or x == 0 then
x = 9
end
if not z or z == 0 then
z = 9
end
area = x*z
if area > 1024 then
term.clear()
print("Too big")
os.sleep(4)
os.reboot()
end
Edited on 15 July 2014 - 10:21 PM
154 posts
Location
London, England
Posted 16 July 2014 - 12:21 AM
Inititalise NBRseeds as local NBRseeds = 0, should fix it
7 posts
Posted 16 July 2014 - 12:27 AM
The error was very simple!!
Thank you
154 posts
Location
London, England
Posted 16 July 2014 - 12:30 AM
The error was very simple!!
Thank you
99% of the time they are
The other 1% they require a complete rewrite…