Jump to content




Reading a variable as a string


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

#1 The_Awe35

  • Members
  • 71 posts
  • LocationNerdfightaria Island

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.

#2 OmegaVest

  • Members
  • 436 posts

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


#3 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users