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

read() and write() question

Started by ryryanator, 01 July 2015 - 09:15 PM
ryryanator #1
Posted 01 July 2015 - 11:15 PM
So im trying to make a program that can take user input and give out a string.


term.clear()
term.setCursorPos(1,1)
write("What are you looking for\n")
local input = read()
local wood = "1.1"
local coal = "1.2"
while true do
write(input)
sleep(5)
shell.run("disk/database")
end

of course, when this runs, it just outputs whatever you wrote in. how can i get it so that it will output the string coal or wood if thats what the user typed in?

Thanks!
Edited on 02 July 2015 - 12:44 AM
Dragon53535 #2
Posted 02 July 2015 - 03:57 AM

if input == 'wood' then
  input = "1.1"
elseif input == 'coal' then
  input = '1.2'
end
Anavrins #3
Posted 02 July 2015 - 05:01 AM
I'd go with something like this

local materials = {
    wood = "1.1",
    coal = "1.2",
}
local input = read()
print(materials[input] or "Material not found")
ryryanator #4
Posted 02 July 2015 - 03:23 PM
I'd go with something like this

local materials = {
	wood = "1.1",
	coal = "1.2",
}
local input = read()
print(materials[input] or "Material not found")

This is what i was looking for! thanks much!