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

[Programming][Lua] Having some trouble with conditional statements

Started by Laywan, 26 September 2012 - 06:29 PM
Laywan #1
Posted 26 September 2012 - 08:29 PM
First off, i am very new to programming (this is the first program i have ever written) so i would appreciate some tips.

In regards to my issue, this program is supposed to ask what you want to build for example a walkway,
but when i type in Walkway the program stops there instead of starting to build said walkway.

I'm guessing the problem lies with the if then statement, and that the problem is very easy to solve, but sadly i just can not figure it out…

If you can see some room for improvement somewhere other than my issue i would be glad to hear it.


local build
local ww

function buildWW()
  turtle.select(1)
  turtle.forward()
  turtle.turnLeft()
  turtle.placeDown()
  turtle.place()
  turtle.turnRight()
  turtle.turnRight()
  turtle.forward()
  turtle.placeDown()
  turtle.forward()
  turtle.placeDown()
  turtle.place()
  turtle.turnLeft()
  turtle.turnLeft()
  turtle.forward()
  turtle.forward()
  turtle.turnRight()
end


term.clear()
term.setCursorPos(1,1)
term.write("What should i build? ")
build = read()
term.clear()
term.setCursorPos(1,1)

if build == Walkway then
  term.clear()
  term.setCursorPos(1,1)
  term.write("How long should the walkway be? ")
  ww = read()
  term.clear()
  term.setCursorPos(1,1)
  print("Building walkway " ..ww.. " rows long")
  local rows = ww
 
  for i = 1,ww do
	buildWW()	  
	print(rows.. " rows left")
	rows = rows-1
  end
  
elseif build == House then
  print("Building house")
end
GopherAtl #2
Posted 26 September 2012 - 08:33 PM
In the if and elseif statements, Walkway and House need to be in quotes, like "Walkway" and "House" The quotes tell Lua you mean the actual words Walkway and House, rather than the value of variables named Walkway and House.
Laywan #3
Posted 26 September 2012 - 10:38 PM
ah, i see, i did not know that. thanks for the help
Laywan #4
Posted 27 September 2012 - 12:39 PM
I'm having a new problem i can't figure out… When i start the program it calls the function building(), and then you can tell it what to build, or ask for a list of builds,
but when i call the list of builds it should call the function building() again, and you can write in for example walkway. but instead of starting the walkway program it just stops

Spoiler
local build
local ww
local DHXY
local DHZ

function building()
  term.clear()
  term.setCursorPos(1,1)
  term.write("What should i build? ")
  build = read()
  term.clear()
  term.setCursorPos(1,1)
end

function checkstone()
  turtle.getItemCount(3)
  ItemCount1 = turtle.getItemCount(3)
  turtle.getItemCount(4)
  ItemCount2 = turtle.getItemCount(4)
  turtle.getItemCount(5)
  ItemCount3 = turtle.getItemCount(5)
  turtle.getItemCount(6)
  ItemCount4 = turtle.getItemCount(6)
  turtle.getItemCount(7)
  ItemCount5 = turtle.getItemCount(7)
  turtle.getItemCount(8)
  ItemCount6 = turtle.getItemCount(8)
  turtle.getItemCount(9)
  ItemCount7 = turtle.getItemCount(9)
  turtle.getItemCount(10)
  ItemCount8 = turtle.getItemCount(10)

  if ItemCount1 > 1 then
	turtle.select(3)
  elseif ItemCount2 > 1 then
	turtle.select(4)
  elseif ItemCount3 > 1 then
	turtle.select(5)
  elseif ItemCount4 > 1 then
	turtle.select(6)
  elseif ItemCount5 > 1 then
	turtle.select(7)
  elseif ItemCount6 > 1 then
	turtle.select(8)
  elseif ItemCount7 > 1 then
	turtle.select(9)
  elseif ItemCount8 > 1 then
	turtle.select(10)
  else
	print("Out of Bricks")
  end
end


function list()
  term.clear()
  term.setCursorPos(1,1)
  print("Words are case sensitive")
  term.setCursorPos(1,3)
  print("walkway")
  print("direhouse")
  os.sleep(5)
  building()
end

function buildWW()
  turtle.forward()
  turtle.turnLeft()
  checkstone()
  turtle.placeDown()
  checkstone()
  turtle.place()
  turtle.turnRight()
  turtle.turnRight()
  turtle.forward()
  checkstone()
  turtle.placeDown()
  turtle.forward()
  checkstone()
  turtle.placeDown()
  checkstone()
  turtle.place()
  turtle.turnLeft()
  turtle.turnLeft()
  turtle.forward()
  turtle.forward()
  turtle.turnRight()
end

function walkway()
  term.clear()
  term.setCursorPos(1,1)
  term.write("How long should the walkway be? ")
  ww = read()
  term.clear()
  term.setCursorPos(1,1)
  print("Building walkway " ..ww.. " rows long")
  local rows = ww
  for i = 1,ww do
	checkFuel()
	buildWW()	  
	print(rows.. " rows left")
	rows = rows-1
  end
end

function buildDH()
end

function direhouse()
  term.clear()
  term.setCursorPos(1,1)
  term.write("How large should the house be? ")
  DHXY = read()
  term.setCursorPos(1,3)
  term.write("How tall should the house be? ")
  DHZ = read()
  os.sleep(3)
  term.clear()
  term.setCursorPos(1,1)
  print("Building house " ..DHXY.. "x" ..DHXY.. " and " ..DHZ.. " tall")
end

building()
if build == "walkway" then
  walkway()  
elseif build == "direhouse" then
  direhouse()
elseif build == "list" then
  list()
else
  print("Input not accepted")
  os.sleep(3)
  building()
end

http://pastebin.com/B7yzuZaj

and also does anyone know a better way to write the checkstone function i have?
fusiomax #5
Posted 27 September 2012 - 01:28 PM
I just copied this over and ran it and I get and attempt to call nil, then I noticed In your function walkway() you call checkFuel() but you havn't defined a function called checkFuel() as far as I can see.

Edit: I just added an empty function called checkFuel() and it ran fine for me.
Laywan #6
Posted 27 September 2012 - 01:32 PM
i had a function called that before, just forgot to remove it before i pasted it here.
it's not a part of the issue i'm having

edit: yeah it runs fine, but when i type in for example list, and it goes back to the what to build part, you can't write in list again, it just stops the program
fusiomax #7
Posted 27 September 2012 - 01:39 PM
i had a function called that before, just forgot to remove it before i pasted it here.
it's not a part of the issue i'm having

edit: yeah it runs fine, but when i type in for example list, and it goes back to the what to build part, you can't write in list again, it just stops the program

on line 137 where you start your if command to determine what to do with the user input it only runs once, if you add it inside a loop eg:


while true do
--code
end

it would continually run, you could also then add in a command (exit) that would terminate the program

I added this basic loop in and it worked fine, I could run each command as many times as I wanted (though had to use ctrl+t to exit the program)
Laywan #8
Posted 27 September 2012 - 01:46 PM
ah awesome. Thanks
Fatal_Exception #9
Posted 27 September 2012 - 01:53 PM
A better way of doing your checkstone() is to use a for loop:
function checkstone()
  for i = 3, 10 do
	if turtle.getItemCount(i) > 1 then
	  turtle.select(i)
	  return
	end
  end
  print("Out of Bricks")
end