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

"While" and "Until" help

Started by ChaddJackson12, 01 October 2012 - 11:45 PM
ChaddJackson12 #1
Posted 02 October 2012 - 01:45 AM
Please help me with the following code if possible:



print("Press [Enter] To Unlock!")
event, key = os.pullEvent("key")
while not key == 13 do -- 13 is the key ENTER
os.sleep(0.1)
until key == 13 then
print("YAY")
end

It gives an error at startup, about a 'too close 'while' at line …' I cut this out of some code so the number would be pointless to give, but if possible please help or correct this code. Thanks.
ChaddJackson12 #2
Posted 02 October 2012 - 01:48 AM
Please help me with the following code if possible:



print("Press [Enter] To Unlock!")
event, key = os.pullEvent("key")
while not key == 13 do -- 13 is the key ENTER
os.sleep(0.1)
until key == 13 then
print("YAY")
end

It gives an error at startup, about a 'end expected, (too close 'while' at line #)' I cut this out of some code so the number would be pointless to give, but if possible please help or correct this code. Thanks.
I am using notepad++ and I have closed the function it is in, and that is the only other thing that needs to be closed. I did try closing it but it seems as if "until" closes it as well… If that helps you.
Fatal_Exception #3
Posted 02 October 2012 - 02:06 AM
While and Until are two separate types of loop, and Until .. then is not valid syntax.

print("Press [Enter] To Unlock!")
repeat
  event, key = os.pullEvent("key")
  os.sleep(0.1)
until key == 13 -- 13 is the key ENTER
print("YAY")
MysticT #4
Posted 02 October 2012 - 02:07 AM
You got the while syntax wrong:

while <condition> do
  -- code
end
-- or repeat:
repeat
  -- code
until <condition>
So your code should be:

function someFunction() -- you said it's on a function
  print("Press [Enter] To Unlock!")
  repeat
	local event, key = os.pullEvent("key")
    -- removed the sleep, it's not necesary
  until key == 13 then
  print("YAY")
end

Edit: ninja'd :)/>/>
ChaddJackson12 #5
Posted 02 October 2012 - 02:07 AM
While and Until are two separate types of loop, and Until .. then is not valid syntax.

print("Press [Enter] To Unlock!")
repeat
  event, key = os.pullEvent("key")
  os.sleep(0.1)
until key == 13 -- 13 is the key ENTER
print("YAY")

Ah, thanks.
MysticT #6
Posted 02 October 2012 - 02:09 AM
Oh, a little detail: 13 is not the enter key, it's 28.
ChaddJackson12 #7
Posted 02 October 2012 - 02:10 AM
You got the while syntax wrong:

while <condition> do
  -- code
end
-- or repeat:
repeat
  -- code
until <condition>
So your code should be:

function someFunction() -- you said it's on a function
  print("Press [Enter] To Unlock!")
  repeat
	local event, key = os.pullEvent("key")
	-- removed the sleep, it's not necesary
  until key == 13 then
  print("YAY")
end

Edit: ninja'd :)/>/>

Unexpected symbol on the "Until" line.. I copied what you had said… :3

EDIT: fixed, sorry.
MysticT #8
Posted 02 October 2012 - 02:12 AM
Derp, didn't see that you put a then after that. It should be:

function someFunction() -- you said it's on a function
  print("Press [Enter] To Unlock!")
  repeat
	local event, key = os.pullEvent("key")
  until key == 28 -- 28 is the enter key
  print("YAY")
end
ChaddJackson12 #9
Posted 02 October 2012 - 02:13 AM
Oh, a little detail: 13 is not the enter key, it's 28.
Wow, thanks, I was just BARELY experiencing this problem. What had happened is I had found the correct key but got it confused from a thing for adobe keyboard IDs, which I though it was a static id for everything.
MysticT #10
Posted 02 October 2012 - 02:15 AM
Oh, a little detail: 13 is not the enter key, it's 28.
Wow, thanks, I was just BARELY experiencing this problem. What had happened is I had found the correct key but got it confused from a thing for adobe keyboard IDs, which I though it was a static id for everything.
Minecraft (and CC) uses this key codes. In CC 1.4 there's a keys api that makes it easier.