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

Variable Bug

Started by Truitedu18, 24 March 2013 - 11:40 AM
Truitedu18 #1
Posted 24 March 2013 - 12:40 PM
I have a strange bug with this code :
Spoilerlocal function creuser(j)
turtle.dig()
turtle.forward()
if j == 4 then
turtle.turnRight()
turtle.turnRight()
turtle.select(1)
turtle.place()
turtle.turnRight()
turtle.turnRight()
j = 0
else
j = j + 1
end
turtle.turnLeft()
turtle.dig()
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.digUp()
turtle.up()
turtle.dig()
turtle.turnLeft()
turtle.turnLeft()
turtle.dig()
turtle.digUp()
turtle.up()
turtle.dig()
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.turnLeft()
turtle.dig()
turtle.forward()
turtle.turnLeft()
turtle.dig()
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.digDown()
turtle.down()
urtle.dig()
turtle.turnLeft()
turtle.turnLeft()
turtle.dig()
turtle.digDown()
turtle.down()
turtle.dig()
turtle.turnRight()
turtle.turnRight()
turtle.dig()
turtle.turnLeft()
return j
end

function carburant()
if turtle.getFuelLevel() < 6 then
turtle.select(2)
turtle.refuel()
end
end
shell.run('clear')
print("Veuillez mettre les torches dans le premier slot et le carburant dans le second\n")
print("Veuillez entrer la distance a creuser : ")
local i = io.read()
local j = 8
local continuer = "true"

while continuer do
if (i < 0) or (i == 0) then
continuer = "false"
end
carburant()
j = creuser(j)
i = i - 2
end

This is a code to do a tunnel and place torch. But I have this error :
tunnel_v1:71: attempt to compare number with string expected, got number
I think is due to the variable i that is string however I enter a number.

Thanks
Cloudy #2
Posted 24 March 2013 - 12:57 PM
read() returns a string.
MysticT #3
Posted 24 March 2013 - 12:58 PM
As Cloudy said, read returns a string. You can use tonumber to convert it to a number:

local input = tonumber(read())
Truitedu18 #4
Posted 24 March 2013 - 01:16 PM
Thank you very good :D/>