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

Unknown failure in code and while not give a reason

Started by commanderbass, 09 June 2013 - 08:57 PM
commanderbass #1
Posted 09 June 2013 - 10:57 PM
function soiltest()

x=100
while x=100
turtle.dig()
turtle.turnLeft()
turtle.dig()
turtle.turnLeft()
turtle.dig()
turtle.turnLeft()
turtle.dig()
turtle.turnLeft()
turtle.digDown()
turtle.down()
x=x+1
end

while x=200
turtle.up()
x=x+1
end

while x=300
turtle.turnLeft()
turtle.forward()
turtle.forward()
end
:
Lyqyd #2
Posted 10 June 2013 - 11:01 AM
Split into new topic.

Please post your whole code and the exact text of any error messages you receive while running it. For instance, this should say something like, "`end` expected to close `function` at line 1".

Also, if you want to repeat actions a certain number of times, use a for loop:


for i = 1, 100 do
  turtle.forward()
end
Bomb Bloke #3
Posted 10 June 2013 - 11:22 AM
x=100
while x=100 do  -- You need to specify "do" here.
.              
.
.
x=x+1     -- This while "loop" will run exactly once, 'cause now x = 101, not 100.
end

while x=200  -- There's no way x will equal 200 at this point. It can only be 101. So this "loop" won't run at all.
.
x=x+1	
end
Engineer #4
Posted 10 June 2013 - 03:22 PM
x=100
while x=100 do  -- You need to specify "do" here.
.			  
.
.
x=x+1	 -- This while "loop" will run exactly once, 'cause now x = 101, not 100.
end

while x=200  -- There's no way x will equal 200 at this point. It can only be 101. So this "loop" won't run at all.
.
x=x+1	
end

If you want to check something, you need a double equals sign. So replace the while x = 100 do with

while x == 100 do
GopherAtl #5
Posted 10 June 2013 - 04:06 PM
Since we're each pointing out one error, I'll add that he forgot an end at the end.
Lyqyd #6
Posted 10 June 2013 - 04:17 PM
I'm beginning to think that either nobody reads what I write in my "split" posts, or I'm being too understated in my suggested corrections. :P/>
GopherAtl #7
Posted 10 June 2013 - 04:54 PM
oops. I skimmed over the 2nd paragraph, noted that you'd pointed out for loops were prefered and requested the full code but not registered the forgotten end being noted. I do read your posts… as well as I read anyone's…lol
Engineer #8
Posted 10 June 2013 - 05:25 PM
I'm beginning to think that either nobody reads what I write in my "split" posts, or I'm being too understated in my suggested corrections. :P/>

Put your answer in bold :P/> We should read it though xD
Bomb Bloke #9
Posted 10 June 2013 - 06:38 PM
Don't get me wrong, I'd prefer "for" loops here too, but it's worth understanding why the original code doesn't work.

Embarrissing that I missed that equals sign, though.
commanderbass #10
Posted 10 June 2013 - 10:22 PM
Sorry for not posting full code however it did not run at all and so it did not give me a error code. :ph34r:/>
However i tried some of the replies and updated the code and now it gives me this error: bios:338: [string "testsoil"] : 20: 'do'
New code paste bin get QMu0aGpn testsoil
New code:
function testsoil()
x=100
while x>99 do
turtle.dig()
turtle.turnLeft()
turtle.dig()
turtle.turnLeft()
turtle.dig()
turtle.turnLeft()
turtle.dig()
turtle.turnLeft()
turtle.digDown()
turtle.down()
x=x+1
end
while x>199 do
turtle.up()
x=x+1
end
while x=300 do
turtle.turnLeft()
turtle.forward()
turtle.forward()
end
Orwell #11
Posted 10 June 2013 - 10:29 PM
* snip *
As suggested, change:
while x=300 do
to:
while x==300 do

You use '==' for comparisons.
Bomb Bloke #12
Posted 11 June 2013 - 04:06 AM
In addition to the equals sign thing, the code logic is still messed up. This:

while x>99 do
.
.
.
x=x+1	
end

… has an issue where 'x' will always be more then 99. You can keep incrementing it all you like, but that won't ever change (… depending on how Lua handles overflows, I suppose, but don't worry about that. ;)/> ).

Here's another example of how 'for' loops might come in to play here:

for i=1,100 do

  for j=1,4 do
    turtle.dig()
    turtle.turnLeft()
  end	

  turtle.digDown()
  turtle.down()
end

The first time the left-most 'for' block runs, 'i' is set to 1. When the block completes, it automatically runs again, and 'i' is incremented by 1. This continues until 'i' would exceed 100, at which point the program continues from the bottom of the 'for' block.

The nested 'for' loop uses 'j' to track four iterations of the digging/turning process.
commanderbass #13
Posted 11 June 2013 - 12:15 PM
In addition to the equals sign thing, the code logic is still messed up. This:

while x>99 do
.
.
.
x=x+1	
end

… has an issue where 'x' will always be more then 99. You can keep incrementing it all you like, but that won't ever change (… depending on how Lua handles overflows, I suppose, but don't worry about that. ;)/> ).

Here's another example of how 'for' loops might come in to play here:

for i=1,100 do

  for j=1,4 do
	turtle.dig()
	turtle.turnLeft()
  end	

  turtle.digDown()
  turtle.down()
end

The first time the left-most 'for' block runs, 'i' is set to 1. When the block completes, it automatically runs again, and 'i' is incremented by 1. This continues until 'i' would exceed 100, at which point the program continues from the bottom of the 'for' block.

The nested 'for' loop uses 'j' to track four iterations of the digging/turning process.
Please restate in Simple English.
Lyqyd #14
Posted 11 June 2013 - 12:45 PM
That was pretty simple, if you take the time to read it carefully. Essentially, though, a while loop will only keep looping while its condition evaluates true. So a loop like:


x = 100
while x == 100 do
  x = x + 1
end

Will only run once. The first time the loop starts, x is 100, so its condition ( x == 100 ) is true. Then in the loop body, we add 1 to x, making it 101. When the loop tries to start again, its condition evaluates to false (101 does not equal 100), so it skips the loop and moves on. Your next attempt was something like:


x = 100

while x > 99 do
  x = x + 1
end

while x > 199 do
  x = x + 1
end

The issue here is that your first loop will never end. When the first loop begins, x is greater than 99. Since the loop adds one to x, it will always be greater than 99, so that first loop will keep looping forever. That code will never reach the second loop. So we suggested you use a for loop instead. For loops can be made to loop a specific number of times, which is what it seems like you're looking for. Something like this:


for i = 1, 100 do --# Loop from one to one hundred
  turtle.forward()
end
commanderbass #15
Posted 11 June 2013 - 08:20 PM
That was pretty simple, if you take the time to read it carefully. Essentially, though, a while loop will only keep looping while its condition evaluates true. So a loop like:


x = 100
while x == 100 do
  x = x + 1
end

Will only run once. The first time the loop starts, x is 100, so its condition ( x == 100 ) is true. Then in the loop body, we add 1 to x, making it 101. When the loop tries to start again, its condition evaluates to false (101 does not equal 100), so it skips the loop and moves on. Your next attempt was something like:


x = 100

while x > 99 do
  x = x + 1
end

while x > 199 do
  x = x + 1
end

The issue here is that your first loop will never end. When the first loop begins, x is greater than 99. Since the loop adds one to x, it will always be greater than 99, so that first loop will keep looping forever. That code will never reach the second loop. So we suggested you use a for loop instead. For loops can be made to loop a specific number of times, which is what it seems like you're looking for. Something like this:


for i = 1, 100 do --# Loop from one to one hundred
  turtle.forward()
end
Ok thanks for the tip so before i try to use it let me run by you the new code using the fixes you gave
function testsoil()
x=100
for i = 1, 100 do –# Loop from one to one hundred
turtle.dig()
turtle.turnLeft()
turtle.dig()
turtle.turnLeft()
turtle.dig()
turtle.turnLeft()
turtle.dig()
turtle.turnLeft()
turtle.digDown()
turtle.down()
x=x+1
end
for i = 1, 100 do –# Loop from one to one hundred
turtle.up()
x=x+1
end
while x==300
turtle.turnLeft()
turtle.forward()
turtle.forward()
end
Bomb Bloke #16
Posted 12 June 2013 - 03:59 AM
Putting aside the number of 'end' statements in there for the moment (there should be an extra one at the bottom to finish the function!), the above will work up until the end.

The first 'for' loop will run a hundred times, by which time 'x' will've incremented to a total of 200. The second 'for' loop will try to move the turtle up a hundred times, at the end of which 'x' will be 300.

The final 'while' loop won't work at all because you've omitted the 'do' again. If you add that in there it'll run forever, as you don't change 'x' further, and so it'll always be 300.
commanderbass #17
Posted 12 June 2013 - 09:06 AM
Putting aside the number of 'end' statements in there for the moment (there should be an extra one at the bottom to finish the function!), the above will work up until the end.

The first 'for' loop will run a hundred times, by which time 'x' will have incremented to a total of 200. The second 'for' loop will try to move the turtle up a hundred times, at the end of which 'x' will be 300.

The final 'while' loop won't work at all because you've omitted the 'do' again. If you add that in there it'll run forever, as you don't change 'x' further, and so it'll always be 300.
Thank you so much i implemented the changes you suggested however now when i try it it will not work period? Could it be that i am using pastebin to download? thank you so much in advance. Here is the pastebin code QMu0aGpn.
Lyqyd #18
Posted 12 June 2013 - 10:33 AM
Are you calling the function?
commanderbass #19
Posted 12 June 2013 - 12:23 PM
Are you calling the function?
I should be i type in pastebin get …. testsoil.
it saves i make sure it is in the programs list then type testsoil
it then does nothing.
Engineer #20
Posted 12 June 2013 - 12:41 PM
Right now you only have the function. This on its own does nothing, you have to actually call it before that code runs. Just like Lyqyd already pointed out
Lyqyd #21
Posted 12 June 2013 - 12:42 PM
Sounds like you're not calling the function. You declare all this code in a function body in your file, but then never call the function. Try putting this below everything else in the file:

testsoil()
commanderbass #22
Posted 12 June 2013 - 12:49 PM
Right now you only have the function. This on its own does nothing, you have to actually call it before that code runs. Just like Lyqyd already pointed out
Okay thanks for the tip. So how do i do that?
Engineer #23
Posted 12 June 2013 - 01:27 PM

function hi()
  print("hey")
end

hi()

Edit: I didnt notice you Lyqyd! D:
Sounds like you're not calling the function. You declare all this code in a function body in your file, but then never call the function. Try putting this below everything else in the file:

testsoil()
That is basically it :)/>
commanderbass #24
Posted 12 June 2013 - 01:51 PM

function hi()
  print("hey")
end

hi()

Edit: I didnt notice you Lyqyd! D:
Sounds like you're not calling the function. You declare all this code in a function body in your file, but then never call the function. Try putting this below everything else in the file:

testsoil()
That is basically it :)/>
OMG thank you it works now here is pastebin code for final product use it in anyway you guys want just give me a shout out.
QMu0aGpn
commanderbass #25
Posted 12 June 2013 - 02:48 PM
Just had an idea. Is there any way to make a computer activate multiply turtles with this program. I am open to any ideas.