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

'if' Statement Isnt Working Correctly?

Started by MagicLegend, 30 July 2013 - 09:09 AM
MagicLegend #1
Posted 30 July 2013 - 11:09 AM
Hello everyone,

I was playing around in my modded survival world and wanted to make a turtle setting up a buildcraft quarry for me. So i started coding a little while ago, and made a pretty simple program (The first few lines of code) that could based on user input move blocks on the X and Y axel (according to translate.google.com its called that in english…). Now i wanted to improve the code, but when i was coding for a bit i got a bug in the program wich i havent been able to fix (yet). Pastebin: http://pastebin.com/PJWLPNWi

Code in a spoiler:

SpoilerNote that i commented out a bit part of the program to test, and that the turtle needs the same block in his first slot that is under him. It's not the most compact code in the world, i'm only a beginner :)/>/>

turtle.select(1)
local Check = "False"

print("Check = "..Check)
--function Startup()
if turtle.compareDown() then
        print("How many times should i loop?")
        local Num = tostring( read() )
        sleep(1)
 term.clear()
 term.setCursorPos(1,1)
 sleep(0.5)
 print("I loop "..Num.." times")
                print("And wich way should i move?")
                print("Usage: Type 'Left' or 'Right'")
                local Move = tostring( read() )
   term.clear()
   term.setCursorPos(1,1)
  print("You chose: "..Move)
  print("Good choice!")
  sleep(1)
  term.clear()
  term.setCursorPos(1,1)

  for i=1,10 do
  print("You chose: "..Move)
  print("Good choice!")
                print("Starting Engines.")
                sleep(0.25)
                term.clear()
                term.setCursorPos(1,1)
  print("You chose: "..Move)           
  print("Good choice!")
  print("Starting Engines..")
                sleep(0.25)
                term.clear()
                term.setCursorPos(1,1)
                print("You chose: "..Move)
  print("Good choice!")
  print("Starting Engines...")
  sleep(0.25)
                term.clear()
                term.setCursorPos(1,1)
        end

else
print("There's no cobble under me/in slot 1!")
end
--end
print(Move)


--[[function Check()
if Move = Left then
  Check = True
  elseif Move = Right then
  Check = True
  else
  Check = False
end

if Check = True then
print("You entered all i needed!")
sleep(1)
print("Lets roll! :D/>/>")
else
print("You entered something wrong!")
sleep(1)
print("Rebooting in 5 seconds")
sleep(1)
term.clear()
term.setCursorPos(1,1)
print("4")
sleep(1)
term.clear()
term.setCursorPos(1,1)
print("3")
sleep(1)
term.clear()
term.setCursorPos(1,1)
print("2")
sleep(1)
term.clear()
term.setCursorPos(1,1)
print("1")
sleep(0.2)
term.clear()
term.setCursorPos(1,1)
print("Goodbye!")
os.reboot()
end
end

function Work() for i=1,Num do
  print("Looped "..tostring(i).." times")
  turtle.forward()
  sleep(0.3)
end

turtle.select(1)
turtle.placeDown()
turtle.up()
turtle.select(2)
turtle.placeDown()
turtle.turnLeft()
turtle.turnLeft()
turtle.forward()
turtle.down()

Num1 = Num-1

print(Num)
print(Num1)

for i=1,Num1 do
  print("Looped "..tostring(i).." times")
  turtle.forward()
  sleep(0.3)
end

turtle.turnRight()

for i=1,Num do
  print("Looped "..tostring(i).." times")
  turtle.forward()
  sleep(0.3)
end

turtle.select(1)
turtle.turnLeft()
turtle.turnLeft()
turtle.placeDown()
turtle.up()
turtle.select(2)
turtle.placeDown()
turtle.forward()
turtle.down()
turtle.select(1)

for i=1,Num1 do
  print("Looped "..tostring(i).." times")
  turtle.forward()
  sleep(0.3)
end

print("Done! Place your quarry! :D/>/>")
end

Startup()
Check()
Work()]]--

It's crashing on line 54, where i want to compare the variable to 'Left' or 'Right'. The error i get is "bios:337: [string "quarry"]:55: 'then' expected". The program is called "quarry". The turtle is labeled and has no peripherals on it. I think i make a really stupid mistake (a typo or something like that) and i just dont see it :)/>/>

ML
Lyqyd #2
Posted 30 July 2013 - 12:53 PM
Split into new topic.

Use the comparison operator (==) instead of the assignment operator (=).
MagicLegend #3
Posted 30 July 2013 - 01:00 PM
thank you so much! I thought that == meant 'has to be equal to' :)/>
TheOddByte #4
Posted 30 July 2013 - 01:06 PM
thank you so much! I thought that == meant 'has to be equal to' :)/>
When you do '=' you set something to what you want

Example

text = "Hello World"
print(text)

Also..

Num = tostring(read())
It should be

Num = tonumber(read())
Since I saw you used it in a loop and the loop can't run from a string

And I saw on a line you set 'True' when it should be 'true'

And when you are using 'read()' for something that isn't numbers you don't need 'tostring()'!