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

local " " = read() and if then not working

Started by Cing, 27 March 2015 - 06:23 PM
Cing #1
Posted 27 March 2015 - 07:23 PM
hi everyone

I have started writing a program for a turtle and had a problem
with the text that I wrote in and if then.
I mad a test program of it.

Pastebin: dFUegsb7

If I typed in 1 the out was:

1
wrong
oke

I have no idea whtat i did wrong could anyone please help?
MKlegoman357 #2
Posted 27 March 2015 - 08:04 PM
The function 'read' returns a string, not a number. You can use the function 'tonumber' to convert a string to a number:


local input = tonumber( read() )

if input then --# if the user entered a number
  print("You entered the number: ", input)
else
  print("You didn't enter a valid number")
end
Cing #3
Posted 27 March 2015 - 08:13 PM
whoo thanks a lot :)/>

But does if then not work with strings?
Bomb Bloke #4
Posted 28 March 2015 - 12:07 AM
tonumber() returns nil if it can't convert its parameter to a numeral, so tonumber(read()) will indeed return nil if anything but a number is typed.
Cing #5
Posted 30 March 2015 - 04:45 PM
Thanks again :D/>

And here is the finalized code: http://www.computercraft.info/forums2/index.php?/topic/22596-ore-mining-turtle-with-enderchest/page__p__211545#entry211545
and pastbin: http://pastebin.com/AfFDnjki