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

Input not worry about capitals etc.

Started by koslas, 21 January 2013 - 12:06 AM
koslas #1
Posted 21 January 2013 - 01:06 AM
I have a quick question, How am I able to make it so if you input something, it doesn't matter about capitals and stuff
For example this requires you to add "if input == "yes" or input == "Yes" etc.
If I were to type "YEs" with this lot of code it would error

Spoiler

told = false
while not told do
  print("Would you like to open the door?")
  input = read()
  if input == "yes" or input == "Yes" then
	rs.setOutput("left", true)
	sleep(3)
	rs.setOutput("left", false)
	told = true
  elseif input == "no" or input == "No" or input == "nO" or input == "NO" then
	rs.setOutput("left", false)
	told = true
  else
	print("Did not reconise")
  end
end

But I want it so I would only need to do this lot of code with 'if input == "yes" then' and it would work with any sort of capilization.

Spoiler

told = false
while not told do
  print("Would you like to open the door?")
  input = read()
  if input == "yes" then
	rs.setOutput("left", true)
	sleep(3)
	rs.setOutput("left", false)
	told = true
  elseif input == "no" then
	rs.setOutput("left", false)
	told = true
  else
	print("Did not reconise")
  end
end

So I would only need to type yes/no once not over and over with all the different combinations for yes/no
Please tell me you understand me
theoriginalbit #2
Posted 21 January 2013 - 01:07 AM
Using lower() on the input string will turn all the characters into their lowercase equivalent…

example:

input = read()
input = input:lower()
if input == "yes" then
-- etc
remiX #3
Posted 21 January 2013 - 01:07 AM
string.lower(read()) to make it all into lower-case




told = false
while not told do
  print("Would you like to open the door?")
  input = string.lower(read())
  if input == "yes" then
        rs.setOutput("left", true)
        sleep(3)
        rs.setOutput("left", false)
        told = true
  elseif input == "no" then
        rs.setOutput("left", false)
        told = true
  else
        print("Did not reconise")
  end
end
koslas #4
Posted 21 January 2013 - 01:18 AM
Also how would I make it so if you entered a number in a small quiz like this:
Spoiler

print("What is 1,000x3?")
input = read()
if input == "3000" or input == "3,000" then
  print("Correct!")
else
  print("Incorrect!")
end
I would have to have the two with commas, as some people use commas in big numbers (every 3rd number from the right) so it wouldn't worry if they did with just the one for example:

Spoiler

print("What is 1,000x3?")
input = read()
if input == "3000" then
  print("Correct!")
else
  print("Incorrect!")
end
And I entered 3,000 in this second program?
koslas #5
Posted 21 January 2013 - 03:33 AM
And also to see if I entered a certain phrase into something so if I needed the answer of True
It would also accept "It is true" as long as it has "true" somewhere in the string?
remiX #6
Posted 21 January 2013 - 04:50 AM
print("What is 1000x3")
input = tonumber(read())

print(input == 3000 and "Correct!" or "Incorrect!") -- advanced way to print
Eric #7
Posted 21 January 2013 - 04:53 AM
string.match works here:


input = read()
if input:lower():match('true') then
    -- string contains true
end
GravityScore #8
Posted 21 January 2013 - 04:59 AM
Also how would I make it so if you entered a number in a small quiz like this:

And I entered 3,000 in this second program?

And also to see if I entered a certain phrase into something so if I needed the answer of True
It would also accept "It is true" as long as it has "true" somewhere in the string?

Both can be done using the Lua string library.

1. You could use the string method gsub. string.gsub replaces all occurrences of a string with another. In this case, you could replace all commas with nothing using the code:

input = read():lower()
input = string.gsub(input, ",", "")

-- OR using a different notation:

input = read():lower():gsub(",", "")
You could also use this to get rid of spaces and other unwanted characters.
2. You can use string.find for this one. String.find searches a string and sees whether a substring exists in it, and if it does exist, it returns its position. Example:

input = read():lower()
if string.find(input, "true") then
  -- true was found in the string!
end

-- OR using a different notation:

input = read():lower()
if input:find("true") then
  -- found!!
end
Eric #9
Posted 21 January 2013 - 05:03 AM
1. You could use the string method gsub. string.gsub replaces all occurrences of a string with another. In this case, you could replace all commas with nothing using the code:

input = read():lower()
input = string.gsub(input, ",", " ")
You could also use this to get rid of spaces and other unwanted characters.
2. You can use string.find for this one. String.find searches a string and sees whether a substring exists in it, and if it does exist, it returns its position. Example:

input = read():lower()
if string.find(input, "true") then
  -- true was found in the string!
end

Don't forget the more idiomatic and concise notation of input:gsub(",", " ") and input:find('true')
GravityScore #10
Posted 21 January 2013 - 05:08 AM
Don't forget the more idiomatic and concise notation of input:gsub(",", " ") and input:find('true')

I always use that one myself, but I thought using the string.meh notation was clearer for this example :P/>

I'll edit it in…
Lyqyd #11
Posted 21 January 2013 - 05:40 AM
It's true that the use of the string metatable to call the methods directly on the string is perfectly acceptable and more concise, but for the purposes of Ask a Pro answers, it's usually best to use the string.func() notation primarily and add a note (if desired) that the more concise method is available, since it leaves the question-asker the option to use the potentially easier to understand less-concise option while also pointing out the more-concise (but perhaps slightly more difficult to follow) option.