Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0

Question

For some reason break isn't exiting this while loop, when I input "exit" it just prints it and continues the loop instead leaving it.

While true do

  Local input = term.read ()

  Term.write (input)

  If input == "exit" then

    Break

  End

End

Share this post


Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 1

I believe term.read returns its input with a newline char (\n) appended to the end of its output. So, typing 'exit' would return 'exit\n'.

One option is to use io.read if you don't need all of the options term.read offers. io.read will return the input without a newline. Or, you can

strip the newline and compare as usual.

local input = term.read()

if input:sub(-2) == 'exit' then
	-- # I believe input:sub(-2) should strip the last two chars (\n) from the input variable...
end

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.