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

if question.

Started by Felype, 17 January 2014 - 08:59 PM
Felype #1
Posted 17 January 2014 - 09:59 PM
Whats wrong with my code? (sorry for noob question)
If i type random2 it runs the else code.

write("Senha: ")
a = read()
if a == "random" then
print("mensage")
if a == "random2" then
print("potato")
end
else
print("Bye bye")
sleep(2)
os.shutdown()
end
theoriginalbit #2
Posted 17 January 2014 - 10:09 PM
this is where indenting helps.


write("Senha: ")
a = read()
if a == "random" then
  print("mensage")
  if a == "random2" then
    print("potato")
  end
else
  print("Bye bye")
  sleep(2)
  os.shutdown()
end

what you meant to do was make the next check an elseif


write("Senha: ")
a = read()
if a == "random" then
  print("mensage")
elseif a == "random2" then
  print("potato")
else
  print("Bye bye")
  sleep(2)
  os.shutdown()
end
Felype #3
Posted 17 January 2014 - 10:17 PM
this is where indenting helps.


write("Senha: ")
a = read()
if a == "random" then
  print("mensage")
  if a == "random2" then
	print("potato")
  end
else
  print("Bye bye")
  sleep(2)
  os.shutdown()
end

what you meant to do was make the next check an elseif


write("Senha: ")
a = read()
if a == "random" then
  print("mensage")
elseif a == "random2" then
  print("potato")
else
  print("Bye bye")
  sleep(2)
  os.shutdown()
end
Oh ok thanks but i can add unlimited elseifs?
Edited on 17 January 2014 - 09:18 PM
MudkipTheEpic #4
Posted 17 January 2014 - 10:27 PM
this is where indenting helps.


write("Senha: ")
a = read()
if a == "random" then
  print("mensage")
  if a == "random2" then
	print("potato")
  end
else
  print("Bye bye")
  sleep(2)
  os.shutdown()
end

what you meant to do was make the next check an elseif


write("Senha: ")
a = read()
if a == "random" then
  print("mensage")
elseif a == "random2" then
  print("potato")
else
  print("Bye bye")
  sleep(2)
  os.shutdown()
end
Oh ok thanks but i can add unlimited elseifs?

Yes, yes you can.


if condition then
--Code
elseif othercondition then
--Other code
elseif differentcondition then
--More code
else
--What to do if all the conditions are false
end --You only need one end. 
Felype #5
Posted 17 January 2014 - 10:32 PM
Ok last question how to make the user press a key to stop a loop without parallels api?
Bomb Bloke #6
Posted 17 January 2014 - 10:57 PM
That depends on what the loop's doing.
Felype #7
Posted 18 January 2014 - 09:51 AM
That depends on what the loop's doing.
THe loop will be like a screensaver with ASCII Art but when u press L it will leave the screensaver.
theoriginalbit #8
Posted 18 January 2014 - 10:43 AM
THe loop will be like a screensaver with ASCII Art but when u press L it will leave the screensaver.
post up your code so far so we can give you advice and specific help on how to do this
Felype #9
Posted 18 January 2014 - 07:18 PM
THe loop will be like a screensaver with ASCII Art but when u press L it will leave the screensaver.
post up your code so far so we can give you advice and specific help on how to do this

--I will not create a cool screensaver in this code this code is only to show what i know.
repeat
term.clear()
print("O  ")
sleep(2)
term.clear()
print(" O")
until
--I Dont know where to put this \/
while true do
local event, param = os.pullEvent()
if event == "L"
os.shutdown()
end
end
Bomb Bloke #10
Posted 18 January 2014 - 07:37 PM
Fortunately that's a simple enough task.

Instead of sleeping, you want to queue timer events. You then wait to see which happens first: Either a timer ends (in which case you update the screen to the next step of your animation), or the user presses a key (in which case the script ends).

Here's a very basic example:

local myTimer = os.startTimer(2)  -- A timer event will go into the event queue in two seconds. "myTimer" records it's number.

local frameCounter = 1

while true do
  local event, param = os.pullEvent()  -- Wait for any event.

  if event == "timer" and param == myTimer then   -- Our two-second timer ended.
    term.clear()
    term.setCursorPos(1,1)

    if frameCounter == 1 then
      frameCounter = 2
      print("O")
    elseif frameCounter == 2 then
      frameCounter = 1
      print(" O")
    end

    myTimer = os.startTimer(2)  -- Start a new timer and record its new ID number.

  elseif event == "key" then  -- Instead of our timer ending, our event was triggered by a keypress.
    break  -- Exit our "while" loop.
  end
end