Jump to content




Text Input in Program

computer lua

4 replies to this topic

#1 CRUGG

  • Members
  • 26 posts
  • Locationpropably in front of his PC

Posted 28 April 2018 - 11:38 AM

Hello! I want to make a text-based Program, which for e.g. prints a message. Then the user can enter something, then it should store the entered Sting into a variable, so I can code something like this after it:
if msg = "install" then
  print("Install..")
  [...]
end
if msg = "uninstall" then
  print("Uninstall..")
  [...]
end
if msg = "exit" then
  print("Exit..")
  [...]
end

My questions are:
- How can I make the text input and save the entered text into a variable
- How can I make it, that something will be executed, if the entered text isn't "install", "uninstall" or "exit"
- How can I make the program exit when entering exit

#2 SquidDev

    Frickin' laser beams

  • Members
  • 1,408 posts
  • LocationDoes anyone put something serious here?

Posted 28 April 2018 - 11:55 AM

View PostCRUGG, on 28 April 2018 - 11:38 AM, said:

How can I make the text input and save the entered text into a variable
You'll want the read function for this:
local msg = read()

View PostCRUGG, on 28 April 2018 - 11:38 AM, said:

How can I make it, that something will be executed, if the entered text isn't "install", "uninstall" or "exit"
If you convert all but the "end if"s into "elseif", you can then have a fallback:
if msg == "install" then --# Your code
elseif msg == "uninstall" then --# Your code
elseif msg == "exit" then  --# Your code
else 
  error("Unknown command!", 0) --# Will print an error and exit
end
Also note the use of double equals (checking equality) rather than single equals.

View PostCRUGG, on 28 April 2018 - 11:38 AM, said:

How can I make the program exit when entering exit
If this is on the top-level/in a loop just use return:
--# ...
elseif msg == "exit" then
  return
else
--# ...
Otherwise you could use error with some friendly message.

#3 CRUGG

  • Members
  • 26 posts
  • Locationpropably in front of his PC

Posted 06 August 2018 - 08:52 AM

View PostSquidDev, on 28 April 2018 - 11:55 AM, said:

View PostCRUGG, on 28 April 2018 - 11:38 AM, said:

How can I make the text input and save the entered text into a variable
You'll want the read function for this:
local msg = read()

View PostCRUGG, on 28 April 2018 - 11:38 AM, said:

How can I make it, that something will be executed, if the entered text isn't "install", "uninstall" or "exit"
If you convert all but the "end if"s into "elseif", you can then have a fallback:
if msg == "install" then --# Your code
elseif msg == "uninstall" then --# Your code
elseif msg == "exit" then  --# Your code
else
  error("Unknown command!", 0) --# Will print an error and exit
end
Also note the use of double equals (checking equality) rather than single equals.

View PostCRUGG, on 28 April 2018 - 11:38 AM, said:

How can I make the program exit when entering exit
If this is on the top-level/in a loop just use return:
--# ...
elseif msg == "exit" then
  return
else
--# ...
Otherwise you could use error with some friendly message.

Hey! I know it's an old topic, but how can I make it so when the command is unknown, it prints something like "Unknown command, please use x, y or z!" and then it goes back to the same questions.
Basically I want the comments in the following code to be replaced with something that's working
--| POINT A
print("Please enter x, y or z")
if msg == "x" then --Code here
elseif msg == "y" then --Code here
elseif msg == "z" then  --Code here
else
  print("Unknown command, please use x, y or z")
  --> GOTO POINT A
end


#4 Stekeblad

  • Members
  • 60 posts
  • LocationSweden

Posted 06 August 2018 - 10:04 AM

Quote

Hey! I know it's an old topic, but how can I make it so when the command is unknown, it prints something like "Unknown command, please use x, y or z!" and then it goes back to the same questions.
Basically I want the comments in the following code to be replaced with something that's working
--| POINT A
print("Please enter x, y or z")
if msg == "x" then --Code here
elseif msg == "y" then --Code here
elseif msg == "z" then  --Code here
else
  print("Unknown command, please use x, y or z")
  --> GOTO POINT A
end

Add a loop
local invalidAnswer = true
while invalidAnswer do
  print("Please enter x, y or z")
  if msg == "x" then
    invalidAnswer = false
    --Code here
  elseif msg == "y" then
    invalidAnswer = false
    --Code here
  elseif msg == "z" then
    invalidAnswer = false
    --Code here
  else
    print("Unknown command, please use x, y or z")
  end
end


#5 Bomb Bloke

    Hobbyist Coder

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

Posted 06 August 2018 - 11:41 AM

If you're intending to use this sort of thing at multiple points within your script, then you might consider bundling it off into a new function:

local function getInput(...)
	while true do
		local result = read()
		
		for i = 1, #arg do if result == arg[i] then return result end end
		
		print("Unknown command, please use one of the below:")

		for i = 1, #arg do print(arg[i]) end
	end
end

print("Please enter x, y or z")

local msg = getInput("x", "y", "z")

If you want to ignore case sensitivity, then you can switch to "read():lower()" on the third line.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users