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

bios:206: [string "train"]:6: 'then' expected

Started by mackintosch, 17 May 2012 - 09:45 AM
mackintosch #1
Posted 17 May 2012 - 11:45 AM
Everytime i run this code:

Print ("Bahnhof")

function start()
Print ("Passwort?")
pswd = io.read
if pswd = 01379000 then
	main()
else
	start()
end
end

function main()
clear
Print ("Ziel?")
Print ("1: Schneeland, 2: Dönerland")
input = io.read
if input = 1 then
	redstone.setoutput ("back", false)
	depart()
end
if input = 2 then
	redstone.setoutput ("back", true)
	depart()
else
	main()
end
end

function depart()
clear
print ("losfahren?")
print ("Y/N")
logic = io.read
if logic = "Y" then
	print("Abfahrt in 15s")
	sleep(15)
	redstone.setoutput ("left", true)
	sleep 1
	redstone.setoutput ("left, false)
else
	start()
end
end

I get the Error in the Title. Whats wrong?
Xtansia #2
Posted 17 May 2012 - 12:38 PM
Lets start:
1.
Print
should be
print

No capital P

2.
io.read
should be
io.read()

You need brackets when calling functions

3. In all your if statements you need 2 ='s ie.
if logic == "Y" then
not
if logic = "Y" then

4. Replace
clear
with
shell.run("clear")

OR
term.clear()
term.setCursorPos(1, 1)

5. You forgot the brackets on your last sleep
sleep 1
should be
sleep(1)

6.
redstone.setoutput
should be
redstone.setOutput
mackintosch #3
Posted 17 May 2012 - 01:08 PM
Thats my corrected code:

print ("Bahnhof")
start()

function start()
print ("Passwort?")
pswd = io.read()
if pswd == 01379000 then
    main()
else
    main()
end
end

function main()
term.clear()
term.setCursorPos(1, 1)print ("Ziel?")
print ("1: Schneeland, 2: Dönerland")
input = io.read()
if input == 1 then
    redstone.setOutput ("back", false)
    depart()
end
if input == 2 then
    redstone.setOutput ("back", true)
    depart()
else
    main()
end
end

function depart()
term.clear()
term.setCursorPos(1, 1)
print ("losfahren?")
print ("Y/N")
logic = io.read()
if logic == "Y" then
    print("Abfahrt in 15s")
    sleep(15)
    redstone.setOutput ("left", true)
    sleep (1)
    redstone.setOutput ("left", false)
else
    start()
end
end

$

but if i enter the correct password it still jumps to the start() function? whats still wrong?
MysticT #4
Posted 17 May 2012 - 01:52 PM
The value returned by read() is a string, you are comparing that with a number. Put quotes around the password:

if pswd == "013970000" then -- string
not

if pswd == 013970000 then -- number

Same in the main function:

...
if input == 1 then
  redstone.setOutput ("back", false)
  depart()
end
if input == 2 then
...
should be:

...
if input == "1" then -- added quotes ("")
  redstone.setOutput ("back", false)
  depart()
end
if input == "2" then -- added quotes ("")
...

And you should use a loop instead of calling the function again, since that will fill the stack and throw an error after some time.
Instead of something like:

function main()
  -- your code
  main()
end
use:

function main()
  while true do
    -- your code
  end
end
MathManiac #5
Posted 18 May 2012 - 12:57 AM
A way for people to solve your code faster if u indent it. ie.
function doStuff(awesomesauce)
  if (awesomesauce == "1234") then
    print("Hallo!")
  end
end
not
function doStuff(awesomesauce)
if (awesomesauce == "1234") then
print("Hallo!")
end
end

It makes code much easier to read, and it is close to impossible to debug a long code without indentions.