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

Then is there, but it still gives me errors

Started by xbeigeninjax, 26 January 2013 - 11:40 AM
xbeigeninjax #1
Posted 26 January 2013 - 12:40 PM
Here is my code:


-- This is the RedSole CC program by xbeigeninjax

write("Welcome to RedSole! Home|RA|Exit|Help: ")
if input = "RA" then do redstoneAct()
if input = "exit" then do exit()
if input = "help" then do help()
end
function welcomeS()
  write("Welcome to RedSole! Home|RA|ExitHelp: ")
    if input = "RA" then do redstoneAct()
    if input = "exit" then do exit()
    if input = "help" then do help()
function redstoneTrue()
write("Which side do you want?(L for left, S for stern, B for bottom, etc.)")
input == read()
if input = "Home" then do welcomeS()
if input = "Exit" then do exit()
if input = "Help" then do help()
if input = "L" 
write("For how long?(inf for infinite)")
if input = "inf" do redstone.setOutput("left",true)
else input = x do redstone.setOutput("left",true) sleep(x)
if input = "R" 
write("For how long?(inf for infinite)")
if input = "inf" do redstone.setOutput("right",true)
else input = x do redstone.setOutput("right",true) sleep(x)
if input = "B" 
write("For how long?(inf for infinite)")
if input = "inf" do redstone.setOutput("bottom",true)
else input = x do redstone.setOutput("bottom",true) sleep(x)
if input = "S" 
write("For how long?(inf for infinite)")
if input = "inf" do redstone.setOutput("back",true)
else input = x do redstone.setOutput("back",true) sleep(x)
if input = "T" 
write("For how long?(inf for infinite)")
if input = "inf" do redstone.setOutput("top",true)
else input = x do redstone.setOutput("top",true) sleep(x)
function redstoneAct()
write("Type T for true and F for false (redstone activation): ")
input == read()
if input = "T" do redstoneTrue()
if input = "F" write("Which side do you want off? (A for all): ")
if input = "Home" do welcomeS()
if input = "Exit" do exit()
if input = "Help" do help()
if input = "A" do redstone.setOutput(false)
if input = "B" do redstone.setOutput("bottom",false)
if input = "L" do redstone.setOutput("left",false)
if input = "R" do redstone.setOutput("right",false)
if input = "S" do redstone.setOutput("back",false)
if input = "T" do redstone.setOutput("top",false)
end
end
-- This is the help section of the code.
function help()
print("Okay, so you need help. But since I am a computer and I only understand Lua, I can only tell you what I think you might ask.") sleep(7)
print("So, first of all, in the welcome screen you can type Home to do nothing(because you're already there), RA for the actual console,") sleep(7)
print("Help to get here, and Exit to exit the program. Once you are in RA, you have a choice of T if you want to activate redstone, or") sleep(7)
print("F if you want to turn off redstone. Both inputs direct you to a panel where you input the side you want the redstone to come out of.") sleep(7)
print("From there (if you chose T), you can choose the duration of the redstone output, and it can be any number, including infinity.") sleep(7)
print("And at any point in the program that you can input a variable, you can input any of the welcome screen options (except in the one your in).") sleep(7)
write("Help Done. Input any welcome option: ")
input == read()
if input = "home" do welcomeS()
if input = "RA" do redstoneAct()
if input = "exit" do exit()
end
end
It says "then expected" in line 4, but I have one there (probably in the wrong place), and probably a world of other errors hidden in there. If you would be so kind as to tell me what is wrong and give me a snippet of the line of code wrong back, I would appreciate it.
theoriginalbit #2
Posted 26 January 2013 - 01:38 PM
if statements use == for equality not = change all the = in the if's to == and u will be good.

Also please use [code][/code] tags to format code nicely.

EDIT: also there is a LARGE lack of end's to the if's. also those do's are wrong.

EDIT #2: input == read() should be input = read() you have your thinking about equals and assignments the wrong way around, = means put this in this variable, == is an equality check
Edited on 26 January 2013 - 02:58 PM
crazyguymgd #3
Posted 26 January 2013 - 03:44 PM
you also probably want to use elseif when you have more than one if statement. Example:


if input == "home" then
  welcomeS()
elseif input == "RA" then
  redstoneAct()
elseif input == "exit" then
  exit()
end

Same thing for all of your if's and the else's with a condition after them in the rest of your code.

–Edit fixed my own dam typos.
theoriginalbit #4
Posted 26 January 2013 - 03:54 PM
you also probably want to use elseif when you have more than one if statement. Example:


if input == "home" do
  welcomeS()
elseif input == "RA" do
  redstoneAct()
elseif input == "exit" do
  exit()
end

Same thing for all of your if's and the else's with a condition after them in the rest of your code.
You might want to fix your own bugs here

if … do ? o.O I think you mean if … then
crazyguymgd #5
Posted 26 January 2013 - 04:01 PM
you also probably want to use elseif when you have more than one if statement. Example:


if input == "home" do
  welcomeS()
elseif input == "RA" do
  redstoneAct()
elseif input == "exit" do
  exit()
end

Same thing for all of your if's and the else's with a condition after them in the rest of your code.
You might want to fix your own bugs here

if … do ? o.O I think you mean if … then

Dam thanks for the catch. My point still stands though, use ifelse!
xbeigeninjax #6
Posted 26 January 2013 - 05:13 PM
if statements use == for equality not = change all the = in the if's to == and u will be good.

Also please use [code][/code] tags to format code nicely.

EDIT: also there is a LARGE lack of end's to the if's. also those do's are wrong.

EDIT #2: input == read() should be input = read() you have your thinking about equals and assignments the wrong way around, = means put this in this variable, == is an equality check

Ok, first, how do I edit the original post? And so I need to get rid of all the "do"s? And how many ends do I need (sorry for being a noob, I am just learning Lua)?

Nvm I figured out how to change the post.
Edited on 26 January 2013 - 04:19 PM
theoriginalbit #7
Posted 26 January 2013 - 05:33 PM
You need an end at the end of each block. Where a block starts with an if, while, function, contains instructions to perform. After these instructions is where the end goes.

EDIT: yes get rid of the do's an if statement is if .<condition> then a while statement is while <condition> do
xbeigeninjax #8
Posted 26 January 2013 - 06:54 PM
You need an end at the end of each block. Where a block starts with an if, while, function, contains instructions to perform. After these instructions is where the end goes.

EDIT: yes get rid of the do's an if statement is if .<condition> then a while statement is while <condition> do

I have gotten everything to work up to line 33, where it needs another "then" somewhere. Here is the somewhat fixed code:


-- This is the RedSole CC program by xbeigeninjax

write("Welcome to RedSole! Home|RA|Exit|Help: ")
  input = read()
if input == "RA" then redstoneAct()
elseif input == "exit" then exit()
elseif input == "help" then help()
end
function welcomeS()
  input = read()
  write("Welcome to RedSole! Home|RA|ExitHelp: ")
    if input == "RA" then redstoneAct()
    elseif input == "exit" then exit()
    elseif input == "help" then help()
end
function redstoneTrue()
write("Which side do you want?(L for left, S for stern, B for bottom, etc.)")
input = read()
if input == "Home" then welcomeS()
  elseif input == "Exit" then exit()
elseif input == "Help" then help()
elseif input == "L" then redstone.setOutput("left",true) redstoneLth()
  elseif input == "R" then redstone.setOutput("right",true) redstoneLth()
  elseif input == "S" then redstone.setOutput("back",true) redstoneLth()
  elseif input == "T" then redstone.setOutput("top",true) redstoneLth()
  elseif input == "B" then redstone.setOutput("bottom",true) redstoneLth()
end
function redstoneLth()
    write("For how long? (inf for infinite): ")
      input = read()
      input = x
      if x == "inf"
      if x ~= "inf" then sleep(x)
end   
function redstoneAct()
write("Type T for true and F for false (redstone activation): ")
input = read()
if input == "T" do redstoneTrue()
elseif input == "F" write("Which side do you want off? (A for all): ")
elseif input == "Home" do welcomeS()
elseif input == "Exit" do exit()
elseif input == "Help" do help()
elseif input == "A" do redstone.setOutput(false)
elseif input == "B" do redstone.setOutput("bottom",false)
elseif input == "L" do redstone.setOutput("left",false)
elseif input == "R" do redstone.setOutput("right",false)
elseif input == "S" do redstone.setOutput("back",false)
elseif input == "T" do redstone.setOutput("top",false)
end
end
-- This is the help section of the code.
function help()
print("Okay, so you need help. But since I am a computer and I only understand Lua, I can only tell you what I think you might ask.") sleep(7)
print("So, first of all, in the welcome screen you can type Home to do nothing(because you're already there), RA for the actual console,") sleep(7)
print("Help to get here, and Exit to exit the program. Once you are in RA, you have a choice of T if you want to activate redstone, or") sleep(7)
print("F if you want to turn off redstone. Both inputs direct you to a panel where you input the side you want the redstone to come out of.") sleep(7)
print("From there (if you chose T), you can choose the duration of the redstone output, and it can be any number, including infinity.") sleep(7)
print("And at any point in the program that you can input a variable, you can input any of the welcome screen options (except in the one your in).") sleep(7)
write("Help Done. Input any welcome option: ")
input == read()
if input = "home" do welcomeS()
if input = "RA" do redstoneAct()
if input = "exit" do exit()
end
end
remiX #9
Posted 26 January 2013 - 08:21 PM
You had a lot of missing ends for the ending of functions…

try this,

Spoiler

-- This is the RedSole CC program by xbeigeninjax

function welcomeS()
	input = read()
	write("Welcome to RedSole! Home|RA|ExitHelp: ")
	if input == "RA" then
		redstoneAct()
	elseif input == "exit" then
		exit()
	elseif input == "help" then
		help()
	end
end

function redstoneTrue()
	write("Which side then you want?(L for left, S for stern, B for bottom, etc.)")
	input = read()
	if input == "Home" then welcomeS()
	elseif input == "Exit" then 
	elseif input == "Help" then help()
	elseif input == "L" then redstone.setOutput("left",true) redstoneLth()
	elseif input == "R" then redstone.setOutput("right",true) redstoneLth()
	elseif input == "S" then redstone.setOutput("back",true) redstoneLth()
	elseif input == "T" then redstone.setOutput("top",true) redstoneLth()
	elseif input == "B" then redstone.setOutput("bottom",true) redstoneLth()
	end
end

function redstoneLth()
	write("For how long? (inf for infinite): ")
	input = read()
	input = x -- " What are you trying to do here? x is undefined...
	if x == "inf" then
	elseif x ~= "inf" then sleep(x) end
end
	
function redstoneAct()
	write("Type T for true and F for false (redstone activation): ")
	input = read()
	if input == "T" then redstoneTrue()
	elseif input == "F" then write("Which side then you want off? (A for all): ")
	elseif input == "Home" then welcomeS()
	elseif input == "Exit" then 
	elseif input == "Help" then help()
	elseif input == "A" then redstone.setOutput(false)
	elseif input == "B" then redstone.setOutput("bottom",false)
	elseif input == "L" then redstone.setOutput("left",false)
	elseif input == "R" then redstone.setOutput("right",false)
	elseif input == "S" then redstone.setOutput("back",false)
	elseif input == "T" then redstone.setOutput("top",false) end
end
-- This is the help section of the code.
function help()
	print("Okay, so you need help. But since I am a computer and I only understand Lua, I can only tell you what I think you might ask.") sleep(7)
	print("So, first of all, in the welcome screen you can type Home to then nothing(because you're already there), RA for the actual console,") sleep(7)
	print("Help to get here, and Exit to exit the program. Once you are in RA, you have a choice of T if you want to activate redstone, or") sleep(7)
	print("F if you want to turn off redstone. Both inputs direct you to a panel where you input the side you want the redstone to come out of.") sleep(7)
	print("From there (if you chose T), you can choose the duration of the redstone output, and it can be any number, including infinity.") sleep(7)
	print("And at any point in the program that you can input a variable, you can input any of the welcome screen options (except in the one your in).") sleep(7)
	write("Help Done. Input any welcome option: ")
	input == read()
	if input = "home" then welcomeS()
	elseif input = "RA" then redstoneAct()
	elseif input = "exit" then exit() end
end

while true do
	term.clear()
	term.setCursorPos(1, 1)
	write("Welcome to RedSole! Home|RA|Exit|Help: ")
	input = read()
	if input == "RA" then
		redstoneAct()
	elseif input == "exit" then
		term.clear()
		term.setCursorPos(1, 1)
		break
	elseif input == "help" then
		help()
	end
end
theoriginalbit #10
Posted 26 January 2013 - 08:54 PM
alternatively you can also do something like this. I have written comments on each line to help you understand why things are being done the way I did them, also this system can easily be expanded, see the comments on how to expand to add more options.

Spoiler


local menu -- this is an empty variable which is given content later, its here to stop nil errors

local tSides = { -- a table of the computer sides, with the letter the user enters on the left, and the side the computer uses on the right
  ["B"] = "bottom",
  ["L"] = "left",
  ["R"] = "right",
  ["S"] = "back",
  ["T"] = "top",
  ["F"] = "front",
}

function welcomeS()
  write("Welcome to RedSole! Home|RA|Exit|Help: ")
end

function redstoneTrue()
  write("Which side do you want?(L for left, S for stern, B for bottom, etc.) ")
  local input = read() -- read the input

  write("For how long?(inf for infinite) ")
  local dur = read() -- read the duration they wish it to run

  if menu[input] then  -- this checks if the input was a menu input
	menu[input]() -- run the function that is stored in the menu table for the users input
  elseif tSides[input] then -- if the user has input a side
	rs.setOutput( tSides[input], true ) -- output the redstone signal on that side
	if dur ~= "inf" then -- if they don't want it to run infinitely
	  sleep( tonumber( dur ) ) -- sleep for the time they wanted to
	  rs.setOutput( tSides[input], false ) -- turn off the redstone signal
	end
  else
	print( "Unknown input" ) -- we don't know what they typed
  end
end

function redstoneAct()
  write("Type T for true and F for false (redstone activation): ")
  local input = read()

  if menu[input] and menu[input] ~= "RA" then -- this checks if the input was a menu input and its not this function
	menu[input]() -- run the function that is stored in the menu table for the users input
  elseif input == "T" then
	redstoneTrue()
  elseif input == "F" then
	write("Which side do you want off? (A for all): ")
	local input = read()
	if tSides[input] then -- if the user has input a side
	  rs.setOutput( tSides[input], true ) -- output the redstone signal on that side
	elseif input == "A" then
	  for _,side in pairs( rs.getSides() ) do -- for all the sides on the computer
		rs.setOutput(side, false) -- turn off the side
	  end
	else
	  print("Unknown input") -- we don't know what they typed
	end
  end
end

-- This is the help section of the code.
function help()
  print("Okay, so you need help. But since I am a computer and I only understand Lua, I can only tell you what I think you might ask.")
  sleep(7)
  print("So, first of all, in the welcome screen you can type Home to do nothing(because you're already there), RA for the actual console,")
  sleep(7)
  print("Help to get here, and Exit to exit the program. Once you are in RA, you have a choice of T if you want to activate redstone, or")
  sleep(7)
  print("F if you want to turn off redstone. Both inputs direct you to a panel where you input the side you want the redstone to come out of.")
  sleep(7)
  print("From there (if you chose T), you can choose the duration of the redstone output, and it can be any number, including infinity.")
  sleep(7)
  print("And at any point in the program that you can input a variable, you can input any of the welcome screen options (except in the one your in).")
  sleep(7)
  print("Help Done.")
end

-- if you want to add more menu options just add the functions in here, making note of the comment in the below code to get these new functions to work

menu = { -- this is all the options we want the user to be able to type, left is what they must type, right is the function that will run when they type it
  ["home"] = welcomeS,
  ["RA"] = redstoneAct,
  ["help"] = help,
  ["exit"] = error, -- this exits the program using the error() function

  -- if you want to add more menu options just add them in here, with the format of the left is what the user types and the right is the function that is used when they write the left
}

-- This is the RedSole CC program by xbeigeninjax
while true do
  welcomeS()
  local input = read()

  if menu[input] and menu[input] ~= "home" then  -- this checks if the input was a menu input and its not this function
	menu[input]() -- run the function that is stored in the menu table for the users input
  else
	print( "Unknown input "..input ) -- we don't know what they typed
  end
end