Jump to content




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

help turtle

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

#1 Cing

  • Members
  • 72 posts

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?

#2 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

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


#3 Cing

  • Members
  • 72 posts

Posted 27 March 2015 - 08:13 PM

whoo thanks a lot :)

But does if then not work with strings?

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.

#5 Cing

  • Members
  • 72 posts

Posted 30 March 2015 - 03:45 PM

Thanks again :D

And here is the finalized code: http://www.computerc...545#entry211545
and pastbin: http://pastebin.com/AfFDnjki





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users