Jump to content




if question.


  • You cannot reply to this topic
9 replies to this topic

#1 Felype

  • Members
  • 43 posts
  • LocationBrazil

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


#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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


#3 Felype

  • Members
  • 43 posts
  • LocationBrazil

Posted 17 January 2014 - 10:17 PM

View Posttheoriginalbit, on 17 January 2014 - 10:09 PM, said:

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 by Felype, 17 January 2014 - 10:18 PM.


#4 MudkipTheEpic

  • Members
  • 639 posts
  • LocationWhere you'd least expect it.

Posted 17 January 2014 - 10:27 PM

View PostFelype, on 17 January 2014 - 10:17 PM, said:

View Posttheoriginalbit, on 17 January 2014 - 10:09 PM, said:

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. 


#5 Felype

  • Members
  • 43 posts
  • LocationBrazil

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?

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 17 January 2014 - 10:57 PM

That depends on what the loop's doing.

#7 Felype

  • Members
  • 43 posts
  • LocationBrazil

Posted 18 January 2014 - 09:51 AM

View PostBomb Bloke, on 17 January 2014 - 10:57 PM, said:

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.

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 January 2014 - 10:43 AM

View PostFelype, on 18 January 2014 - 09:51 AM, said:

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

#9 Felype

  • Members
  • 43 posts
  • LocationBrazil

Posted 18 January 2014 - 07:18 PM

View Posttheoriginalbit, on 18 January 2014 - 10:43 AM, said:

View PostFelype, on 18 January 2014 - 09:51 AM, said:

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


#10 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users