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

Reading a variable as a string

Started by The_Awe35, 05 February 2013 - 11:08 AM
The_Awe35 #1
Posted 05 February 2013 - 12:08 PM
I have been trying to make a adjustable turtle land clearing program that I can change out of the program. I have been doing it like this:

term.write(How high? )
high = read()

But then, when I try to compare it, say


if high < z then
  turtle.digUp()
  turtle.up()
  turtle.digUp()
  turtle.up()
  turtle.digUp()
  turtle.up()
  z = z+3
end

It says that it tried to compare it as a number but got a string. Is there something I can do to fix it?
(I know using it as a for loop will work, but I need it to go up in 3 intervals. the full code is here: http://pastebin.com/mBtMczFJ
Note: The code is messy and flawed as I have been having many problems (mostly from just this one thing) with the code, and I may have some unnecessary variables or code that I was using and forgot to get rid of.
OmegaVest #2
Posted 05 February 2013 - 12:20 PM
You need a tonumber.


term.write("How high?")
high = tonumber(read())

for zed = 1, high, 3 do
   turtle.digUp()
   turtle.Up()

   turtle.digUp()
   turtle.Up()

   turtle.digUp()
   turtle.Up()
end
TheOddByte #3
Posted 06 February 2013 - 12:28 PM
And also if you wanna convert it to a string later then you can do:

high = tostring(high) -- This is if you already have declared what high is.
I've noticed that when I was saving numbers into files I had to convert it into a string
and when loading I had to convert it into a number.. Just Saying.
Spoiler

IF we say var is 1 and when saving it into a file it becomes 1.00 or  1.0 if I don't convert it into a string..
So This Can get you alot of errors in a program..