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

Help with user defined variables for turtles

Started by FortuneSyn, 07 November 2012 - 02:40 AM
FortuneSyn #1
Posted 07 November 2012 - 03:40 AM
Hello,

I just started trying to learn computercraft coding as of last night and I'm completely hooked. Never tried coding anything else before other than like… excel cells lol.

So anyways, I'm trying to learn how to use user defined variables to tell how far a turtle should go/dig/etc. Here is my attempt:


print("Please type how far you want to go")
z = read()
x = 0
while x ~= z do
turtle.forward()
x = x+1
end

However, no matter what input I give it, it will just go forward indefinetly. What am I doing wrong/not understanding? Thanks.
Jan #2
Posted 07 November 2012 - 03:48 AM
Hello,

I just started trying to learn computercraft coding as of last night and I'm completely hooked. Never tried coding anything else before other than like… excel cells lol.

So anyways, I'm trying to learn how to use user defined variables to tell how far a turtle should go/dig/etc. Here is my attempt:


print("Please type how far you want to go")
z = read()
x = 0
while x ~= z do
turtle.forward()
x = x+1
end

However, no matter what input I give it, it will just go forward indefinetly. What am I doing wrong/not understanding? Thanks.
When you enter "7" for example, the computer will do this:

z = "7" -- this is received from the read()
When you compare x to z, it will never be the same because 0 is a number and "7" is a string
to solve this, simply do this:

z = tonumber(read())
sjele #3
Posted 07 November 2012 - 03:48 AM
Read return a string. You are comparing it to a number, fixed code:


print("Please type how far you want to go")
z = tonumber(read())
x = 0
while x ~= z do
turtle.forward()
x = x+1
end

Ninja'd
FortuneSyn #4
Posted 07 November 2012 - 04:02 AM
thankyou!! =) =) =) =)