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

Simple print question,

Started by Avous, 11 January 2013 - 11:45 AM
Avous #1
Posted 11 January 2013 - 12:45 PM
Ok I have a program running a math problem I need it to print the result of two custom inputs can I have it do this?


local result = numone + numtwo

print("R-66Y: Do I have (result here) coal available?")

Obviously i'm referencing two custom entered numbers but how can i make a result and put it into print?

also how can I have it check the fuel amount at any time and terminate if it runs out or automatically refuel itself after a X amount of moves remain?

EDIT: I just figured out how to put the result in after trying different things. I still would like to know how to have it check the fuel and automatically refuel
unobtanium #2
Posted 11 January 2013 - 01:02 PM
Hey,
for the math, this should be a fairly easy one with functions:
Spoiler

local result = 0
local numberone = 0
local numbertwo = 0
function getnumberone()
numberone = read()
end
function getnumbertwo()
numbertwo = read()
end
function addition()
result = numberone + numbertwo
end
function printresult()
print(numberone .. " + " .. numbertwo .. " = " result)
end
getnumberone()
getnumbertwo()
addition()
printresult()

However you can do it smaller without functions:

local result = 0
local numberone = 0
local numbertwo = 0
numberone = read()
numbertwo = read()
result = numberone + numbertwo
print(numberone .. " + " .. numbertwo .. " = " result)

This should be the smallest one:

print(read() + read())

edit:
For the refueling i would recommand a function similar to this one:
Spoiler

local function checkFuel()
if turtle.getFuelLevel() < 1 then
  turtle.select(1)
  turtle.refuel(1)
  print("Refueled!")
end
end

But you would have to add it after every turtle.forward() ect. so it is kinda bad.
So you may use something like this:
Spoiler

local function checkFuel()
if turtle.getFuelLevel() < 1 then
  turtle.select(1)
  turtle.refuel(1)
  print("Refueled!")
end
end
local function forward(j)
for i=1,j,1 do
  turtle.forward()
  checkFuel()
end
end
--If you want the Turle to move one block forward
forward(1)
--If you want the turtle to  move 5 blocks forward
forward(5)

Simply do this for turtle.up() and turtle.down() and turtle.back() if you need them.
Spoiler

local function back(j)
for i=1,j,1 do
  turtle.back()
  checkFuel()
end
end
local function up(j)
for i=1,j,1 do
  turtle.up()
  checkFuel()
end
end
local function down(j)
for i=1,j,1 do
  turtle.down()
  checkFuel()
end
end