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

Variable within while loops.

Started by tshawver11, 18 January 2013 - 09:50 AM
tshawver11 #1
Posted 18 January 2013 - 10:50 AM
I'm trying to use the following code within a program to have the turtles dig a certain number of times that the user can input.
I'm running into a problem however where the x ~=A sections of the code doesn't actually compare the variable x to the variable A. So the while loops becomes an infinite loop and the counter continues to count past A. How can I prevent this issue?

function CountDepth()
if x ~= A then
x = x+1
end
end

while x ~= A do
CountDepth()
end
theoriginalbit #2
Posted 18 January 2013 - 10:52 AM
where do you declare x and A ??
tshawver11 #3
Posted 18 January 2013 - 10:53 AM
It might also be worthy to note that when I put numbers in the spot where A is the program runs the way I want it to.

At the beginning of the program they are both declared I just pulled out the sections of code that were giving me the issue.
theoriginalbit #4
Posted 18 January 2013 - 10:58 AM
Ok I'm going to assume that A is actually read from the user? in which case A would be a string. a number is never equal to a string. so make this change.


A = tonumber( read() )
tshawver11 #5
Posted 18 January 2013 - 11:15 AM
Ah that makes so much sense, and it seemed to do the trick thank you so much.