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

Building turtle program

Started by astonish01, 03 July 2015 - 08:09 PM
astonish01 #1
Posted 03 July 2015 - 10:09 PM
I'm playing on tekkit classic (minecraft 1.2.5) and I'm trying to make a program to building square buildings for me, the problem comes when the building is too high and the blocks in the turtles inventory aren't enough the error it's giving me is
bios:206: [string "build"]:14: 'then' expected
This is the most recent version:

write("How big?")
local Num = tonumber( read() )
write("Height?")
local Hei = tonumber( read() )
for i=1,Hei do
for i=1,4 do
for i=1-1,Num do
turtle.placeDown()
turtle.forward()
end
turtle.turnLeft()
end
turtle.select(1)
if turtle.getItemCount(1) = 0 then
turtle.select(9)
turtle.placeUp
turtle.digUp()
turtle.select(1)
else
turtle.up()
end

There are 2 other version, links: http://pastebin.com/4bBMf52E and http://pastebin.com/Sm8p5BkY
Dog #2
Posted 03 July 2015 - 10:13 PM
In the code you posted, line 14 *should* be:

if turtle.getItemCount(1) == 0 then

= is for setting values
== is for comparing values
astonish01 #3
Posted 03 July 2015 - 10:22 PM
In the code you posted, line 14 *should* be:

if turtle.getItemCount(1) == 0 then

= is for setting values
== is for comparing values

Thanks, if it won't run too much form the topic tittle, can I ask how can I make a simple yes or no prompt thing like the "how big?" or "how high?"?

EDIT: I did some code and came up with this part of the code:

write ("Ready?")
local Rdy = tonumber( read() )
if Rdy == 1 then
 DO SUTFF
else
sleep(30)
end
Edited on 03 July 2015 - 08:40 PM
HPWebcamAble #4
Posted 03 July 2015 - 10:39 PM
how can I make a simple yes or no prompt thing like the "how big?" or "how high?"?

Well, 'how big' or 'how high' isn't a yes or no question, but a yes or no prompt is pretty simple:


print("Do the thing? y/n")  --# Prompt the user to hit y or n

while true do
  local event,key = os.pullEvent("key") --# Wait for the user to hit a key

  if key == keys.y then --# The 'keys' api has all the key codes
    --# Do the thing, end the loop
    break
  elseif key == keys.n then
    --# Do nothing, end the loop
    break
  end
end
astonish01 #5
Posted 03 July 2015 - 10:43 PM
how can I make a simple yes or no prompt thing like the "how big?" or "how high?"?

Well, 'how big' or 'how high' isn't a yes or no question, but a yes or no prompt is pretty simple:


print("Do the thing? y/n")  --# Prompt the user to hit y or n

while true do
  local event,key = os.pullEvent("key") --# Wait for the user to hit a key

  if key == keys.y then --# The 'keys' api has all the key codes
	--# Do the thing, end the loop
	break
  elseif key == keys.n then
	--# Do nothing, end the loop
	break
  end
end

Well, how could I make it in a way it detects the answer, not which key the user hits. Like shown in my edit on the post you answered with the number 1.
Dog #6
Posted 03 July 2015 - 10:54 PM
I'm not clear on what you're asking for here. HPWebcamAble's solution does detect the answer, by getting the key the person presses. If they press 'y' then they're obviously answering 'yes' and if they press 'n' then they're clearly answering 'no'.

The only other solution that comes to mind off the top of my head is this. This is a bit rough…

while true do
  write("Yes or no?")
  local answer = string.lower(read())
  if answer == "yes" or answer == "y" then
    --# do 'yes' stuff
    break
  elseif answer == "no" or answer == "n" then
    --# do 'no' stuff
    break
  else
    write("Invalid response")
  end
end

By using string.lower() I make sure that capitalization doesn't matter which makes the comparison easier. However HPWebcamAble's solution is better for a simple yes/no question - it's easier to write, easier to maintain, easier to read, and easier for the user.
HPWebcamAble #7
Posted 03 July 2015 - 10:55 PM
how could I make it in a way it detects the answer, not which key the user hits. Like shown in my edit on the post you answered with the number 1.

Personally, in a program, I'd rather just hit y or n, not enter a number, but if you prefer the latter, what you put in your previous post will work.
astonish01 #8
Posted 03 July 2015 - 11:29 PM
I'm not clear on what you're asking for here. HPWebcamAble's solution does detect the answer, by getting the key the person presses. If they press 'y' then they're obviously answering 'yes' and if they press 'n' then they're clearly answering 'no'.

The only other solution that comes to mind off the top of my head is this. This is a bit rough…

while true do
  write("Yes or no?")
  local answer = string.lower(read())
  if answer == "yes" or answer == "y" then
	--# do 'yes' stuff
	break
  elseif answer == "no" or answer == "n" then
	--# do 'no' stuff
	break
  else
	write("Invalid response")
  end
end

By using string.lower() I make sure that capitalization doesn't matter which makes the comparison easier. However HPWebcamAble's solution is better for a simple yes/no question - it's easier to write, easier to maintain, easier to read, and easier for the user.

Aparently I got it right by myself, but when I was answering the question I was using a " " before the yes, so it looked like "Ready? yes", instead of "Ready?yes"
Thanks for all :D/>