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

[Question] I can't get it to work

Started by Hidd16, 14 September 2012 - 08:41 PM
Hidd16 #1
Posted 14 September 2012 - 10:41 PM
could some one pls help me with this code i don't know how to get it to work:

function forward(x)
i = 0, x do
turtle.forward()
x=x-1
end

local x
term.write("Please enter how many times: ")
x = read()
print("you entered: "..x)
while input == x do
forward(x)
end

*I want you to be able to say F 23 and then it goes 23 blocks forward and that for each direction
PixelToast #2
Posted 14 September 2012 - 10:46 PM
line 2:
i = 0, x do
needs to be:

for i = 0, x do
you also forgot an end after line 4, and you sould use print instead of term.write
and x needs to be a number and it is easier to localize variables like this:

x = read()
needs to be

local x = tonumber(read())
Cranium #3
Posted 14 September 2012 - 10:47 PM
I have a fairly simple solution to your question. For a "for" loop, you do not need to subtract anything from the value "x". It automatically stops at the end designator. Whenever you declare a variable, you need to use an "=". Such as x = value.

function forward(x)
for i = 1,x do
turtle.forward()
end
end
print("How far do you want to go?")
write("Distance: ")
local dist = read()
forward(tonumber(dist))
Lettuce #4
Posted 14 September 2012 - 11:07 PM
That whole code is mucked up. Your loop is in the wrong place, you set it up wrong, and I don't even know what you're trying to do for x. That's alright. Here's what you need:

forward function:

function forward()
for i = 1,x do
turtle.forward()
end

This loops x times. like if you entered 10, it will go forward 10 times.


term.write "Please enter how many times: "
x = tonumber(read())
print "You entered:"
print(x)

I don't know how to concatenate strings. This will take a user's input, which is a string, and convert it to a number. Then it tells you what that is.

I wrote it down the way you made it, but you should reverse the order. Like so:


term.write "Please enter how many times: "
x = tonumber(read())
print "You entered:"
print(x)

function forward()
for i = 1,x do
turtle.forward()
end

That way, x is defined before forward() calls it. Call forward() by simply typing forward() when you need it.
Usually, I just correct code, but this required a complete rewrite. Keep practicing, you will get better.

Sincerely,
–Lettuce
Cranium #5
Posted 14 September 2012 - 11:19 PM
That way, x is defined before forward() calls it.
You actually don't need to have x defined before the function. when creating a function, anything in the () in "function test(x,f,k)" is a throwaway variable, only used for identification in that variable. You define what variables are being "plugged" into there at any time during your code. So having the function defined first is better, since the variable he is declaring is changed with user input. If it were a variable that is to be used across the entire code, you would want that declared FIRST, but that is not the case.
In your example, you could change it to this:

function forward(x)  --throwaway variable as placeholder for something declared later.
for i = 1,x do
--do something here x number of times
end
end
variable = tonumber(read()) --now declaring a variable
forward(variable) --now calling forward() with variable called earlier.

Oh, and to concatenate strings, it's fairly simple:

string1 = "Concatenate this string "
string2 = " this string."
print(string1.."with"..string2) --add ".." to concatenate between two strings. This can sometimes work with numbers as well, but not always.