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

Problems with my code

Started by OwlBag0410, 08 September 2015 - 01:02 AM
OwlBag0410 #1
Posted 08 September 2015 - 03:02 AM
so basically I'm trying to code a menu screen (I'm very new to computercraft) and whenever I input anything nothing happens, so I have no idea whats wrong.
Here's the code



  while true do
    term.clear()
    term.setBackgroundColor(colors.green)
    term.setCursorPos(1, 1)
    print("Please choose an Action")
    term.setCursorPos(1, 2)
    print("Type actions to show all actions")
    input = read()
    if input == "actions" then
	  print("Open Door")
	  term.setCursorPos(1, 2)
	  print("Current Factory Tests")
    if input == "Open Doo" then
	  shell.run "disk/opendoor"
    if input == "Current Factory Tests" then
	  shell.run "disk/currenttests
    end
    end
    end
  end
Bomb Bloke #2
Posted 08 September 2015 - 04:48 AM
You've got your "if" blocks incorrectly nested inside each other - your script currently only checks if input equals "Current Factory Tests" if it was already found to equal "Open Doo", and only checks if it equals "Open Doo" if it was already found to equal "actions"… Only one of these can be true.

"actions" will lead to the other valid commands being printed, but then your loop immediately repeats, clearing them from the screen…

Make use of elseif:

while true do
	term.clear()
	term.setBackgroundColor(colors.green)
	term.setCursorPos(1, 1)
	print("Please choose an Action")
	term.setCursorPos(1, 2)
	print("Type actions to show all actions")
	input = read():lower()  -- Convert whatever the user types to lowercase.
	if input == "actions" then
		print("Open Door")
		print("Current Factory Tests")
		print("Press any key to continue...")
		os.pullEvent("key")  -- Sit and wait for a keypress.
	elseif input == "open door" then
		shell.run "disk/opendoor"
	elseif input == "current factory tests" then
		shell.run "disk/currenttests
	end
end
OwlBag0410 #3
Posted 09 September 2015 - 09:37 PM
cool thanks alot! :)/>

also if it wasn't too much trouble, the green is only applied to the background of text until an input it put in, is there any way to fix that? It looks a little odd