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

[Lua] [Error] While loop help.

Started by Gamebuster19901, 21 September 2012 - 09:44 PM
Gamebuster19901 #1
Posted 21 September 2012 - 11:44 PM
Sorry for posting a stupid question, but I am still new to computercraft.

What I'm trying to do is display text for 10 seconds, then clear the screen, wait 3 seconds, and repeat, here is the error message.

bios:206: [string "startup"]:4: 'do' expected (I know I have my 'do' in the wrong place, but I don't know where to put it)

and my code


mon = peripheral.wrap("right")
mon.clear()
local x = 1
while x = 1 do
mon.setTextScale(1)
mon.setCursorPos(1,32)
mon.write("This is a very long test sentance")
os.sleep(10)
mon.clear
os.sleep(3)
end


I have tried googling, but everywhere I look 'do' is always after
 x (compare) y 
MysticT #2
Posted 21 September 2012 - 11:54 PM
The problem is that the "(compare)" must be ==, not =.
= assignment
== comparison


local x = 1 -- assign 1 to x
if x == 1 then -- compare x to 1

But you don't need that anyway, since you don't change the value of x, this:

while x == 1 do
is the same as doing:

while true do

Edit: you also forgot the brackets at the second mon.clear call.
MetalMiner #3
Posted 21 September 2012 - 11:57 PM
The 'do' is at the right place.
But you have to use
while x == 1 do
To compare two values you have always to use two =
So your code is:


mon = peripheral.wrap("right")
mon.clear()
local x = 1
while x == 1 do
mon.setTextScale(1)
mon.setCursorPos(1,32)
mon.write("This is a very long test sentance")
os.sleep(10)
mon.clear()
os.sleep(3)
end

But for infinite loops, it's easier to use:

while true do
  print("Your stuff here")
end

Hope I helped
MetalMiner #4
Posted 21 September 2012 - 11:59 PM
Oh, MysticT was faster… :)/>/>
Gamebuster19901 #5
Posted 22 September 2012 - 12:19 AM
Thanks for the help, however I am now recieving this error

startup:2: attemp to index ? (a nil value)

And for my code


mon = peripheral.wrap("right")
mon.clear()
local x = 1
while x == 1 do
mon.setTextScale(1)
mon.SetCursorPos(1,32)
mon.write("this is a very long test sentance")
os.sleep(10)
mon.clear()
os.sleep(3)
end

I also used while x == 1 in another programming language, sorry, I just perfer it that way.
Edited on 21 September 2012 - 10:27 PM
MysticT #6
Posted 22 September 2012 - 12:33 AM
Is the monitor on the right side? Remember that the right side is your right side when you're in front of the computer.
Gamebuster19901 #7
Posted 22 September 2012 - 12:36 AM
Oh, yes it is on the left side, sorry about that, I have the computer covered on 5 sides, so it was hard to tell.
Gamebuster19901 #8
Posted 22 September 2012 - 12:42 AM
Another error,
startup :6:attempt to call nil


mon =peripheral.wrap("left")
mon.clear
local x = 1
while x == 1 do
mon.setTextScale(1)
mon.SetCursorPos(1,32)
mon.write("This is a very long test sentance")
os.sleep(10)
mon.clear()
os.sleep(3)
end

I am probably making the most noob-ish mistakes…
MysticT #9
Posted 22 September 2012 - 12:45 AM
Lua is case-sensitive, so:

mon.SetCursorPos(1, 32)
should be:

mon.setCursorPos(1, 32)
Cranium #10
Posted 22 September 2012 - 12:51 AM
I am probably making the most noob-ish mistakes…
Even the most experienced coder makes a minor spelling mistake here and there once in a while.
Gamebuster19901 #11
Posted 22 September 2012 - 12:54 AM
Thanks, no errors now, but the computer freezes, but I think I can figure out how to fix it, and sorry about the noob mistakes.
MysticT #12
Posted 22 September 2012 - 01:28 AM
Thanks, no errors now, but the computer freezes, but I think I can figure out how to fix it, and sorry about the noob mistakes.
Well, you made an infinite loop, so it won't do anything that's not inside it. If you need to use the computer while it's displaying something in the monitor you'll need to use parallel to run a function and the shell at the same time.