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

input == Not working or I did something wrong...

Started by Brod8362, 29 March 2015 - 04:30 AM
Brod8362 #1
Posted 29 March 2015 - 06:30 AM
I was writing a program for a easy-to-use pocket computer system, until I ran into some other issues (Which I have fixed) until I came across this: the options dont actually work.
What you would do is put in a number to select an option and it runs the code below it, but this just terminates the program. Here is the code:


term.clear()
term.setCursorPos(1,1)
print("1 [Chat]")
print("2 [Shell]")
print("3 [Games]")
print("4 [System Info]")
print("5 [Options]")
print("6 [Reboot]")
print(" ")
print("Selection:")
input = read()
if input == 1 then
shell.run("chat join Chat1 Blake")
elseif input == 2 then
term.clear()
term.setCursorPos(1,1)
shell.run("exit")
elseif input == 3 then
shell.run("games")
elseif input == 4 then
term.clear()
term.setCursorPos(1,1)
shell.run(id)
shell.run(time)
shell.run(drive)
elseif input == 5 then
-- leave blank
elseif input == 6 then
shell.run(reboot)
end
Creator #2
Posted 29 March 2015 - 09:10 AM
Change the line

input = read()

to

input = tonumber(read())

Creator
Edited on 29 March 2015 - 07:11 AM
Dragon53535 #3
Posted 29 March 2015 - 09:14 AM
To explain what creator's saying. read() returns a data type of string, which is a fancy way of saying letters/words/text they're usually identified by having quotes around them, such as inside your print statements. What read returns when you type a number is "1" or "2" which isn't 1 or 2, since "1" and "2" are strings, they aren't the same as 1 and 2 which are numbers. Using tonumber on your read will convert the string into a number which will make it equal 1 or 2. If something is entered that cannot be converted into a number, then tonumber returns nil.
Brod8362 #4
Posted 29 March 2015 - 03:27 PM
Thank you, im a huge noob at lua and this is basically my only experience.