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

Help program doesn't stop

Started by michielewiel, 20 March 2012 - 03:07 PM
michielewiel #1
Posted 20 March 2012 - 04:07 PM
hey i got a little problem
i am making a little shape make script with turtles. but when i tried to test it i came across a problem whenever i try to make a square it ask's me the question what size vertical?
this question belongs to the retangle and not the the square. i think i need to fix it with functions but i don't know how to use them.
here is the code:

print(" shape maker 1.0 beta build made by michiel")
print("what shoud i make?(choose between retangle, triangle, square or rhomedros)")
local choise = io.read()

if choise == "retangle" then
print(" What size horiozontaly?")
sleep(2)
h = io.read()
h = tonumber(h)
end
print("what size vertical?")
v = io.read()
v = tonumber(v)
if h <= 0 then
print("error it can not be 0")
end
if v <= 0 then
print("error it can not be 0")
end
if turtle.detectDown()then
turtle.up()
end
for i=1,v do
print(i)
turtle.placeDown()
turtle.forward()
end
turtle.turnRight()
for i=1,v do
print(i)
turtle.placeDown()
turtle.forward()
end
turtle.turnRight()
os.shutdown()

if choise == "square" then
print("how long does it need to be?")
end
local s = io.read()
local x = s-1
s = tonumber(s)
x = tonumber(x)
if s <= 0 then
print("error length can not be 0")
sleep(2)
end
if turtle.detectDown()then
turtle.up()
end
for i=1,s do
print(i)
turtle.placeDown()
turtle.forward()
end
turtle.back()
turtle.turnRight()
for i=1,s do
print(i)
turtle.placeDown()
turtle.forward()
end
turtle.back()
turtle.turnRight()
for i=1,s do
print(i)
turtle.placeDown()
turtle.forward()
end
turtle.back()
turtle.turnRight()
for i=1,x do
print(i)
turtle.placeDown()
turtle.forward()
end
os.shutdown()

anyway sorry for taking your time and i promiss this will be my last stupid question. so bye bye and already thank you
michiel
Liraal #2
Posted 20 March 2012 - 04:44 PM
first, use the 'code' brackets, just to make it more clear to view.
and your issue is that you close the first 'if' too soon and it executes the rest of the code regardless of the 'if' result. What you need is:

print(" shape maker 1.0 beta build made by michiel")
print("what shoud i make?(choose between retangle, triangle, square or rhomedros)")
local choise = io.read()
if choise == "retangle" then
print(" What size horiozontaly?")
sleep(2)
h = io.read()
h = tonumber(h)
print("what size vertical?")
v = io.read()
v = tonumber(v)
if h <= 0 then
print("error it can not be 0")
end
if v <= 0 then
print("error it can not be 0")
end
if turtle.detectDown()then
turtle.up()
end
for i=1,v do
print(i)
turtle.placeDown()
turtle.forward()
end
turtle.turnRight()
for i=1,v do
print(i)
turtle.placeDown()
turtle.forward()
end
turtle.turnRight()
os.shutdown()
end
if choise == "square" then
print("how long does it need to be?")
local s = io.read()
local x = s-1
s = tonumber(s)
x = tonumber(x)
if s <= 0 then
print("error length can not be 0")
sleep(2)
end
if turtle.detectDown()then
turtle.up()
end
for i=1,s do
print(i)
turtle.placeDown()
turtle.forward()
end
turtle.back()
turtle.turnRight()
for i=1,s do
print(i)
turtle.placeDown()
turtle.forward()
end
turtle.back()
turtle.turnRight()
for i=1,s do
print(i)
turtle.placeDown()
turtle.forward()
end
turtle.back()
turtle.turnRight()
for i=1,x do
print(i)
turtle.placeDown()
turtle.forward()
end
os.shutdown()
end

note that i moved the 'end's to after the os.shutdown() command to make sure all instructions go into their proper blocks.
michielewiel #3
Posted 20 March 2012 - 04:50 PM
thanks a lot
it worked perfectly. i also now get from where my fault was coming ;p
a good day futher
with best regards, michiel