15 posts
Posted 14 November 2012 - 05:35 PM
So I am making a calculator, and when you open the calculator program it displays all the functions you can use and asks you to input one. I use
input = read()
to get the function that the user inputted. My next piece of code is
if input == "Add" then
but I want it so that it isn't case sensitive. I tried
if input == "Add" or "add" then
but that just made it so that you could enter any word or phrase and it would activate the if statement. Could anyone tell me how I would make it so that I could make it non case sensitive?
504 posts
Location
Seattle, WA
Posted 14 November 2012 - 05:44 PM
When you use the condition
or "add" then
The interpreter considers this as: "Is the string "add" nil? It's not nil? Oh, then this condition is true. Let's execute the if body!"
If you want to keep your current code you could rewrite it as:
if input == "Add" or input == "add" then
Or…
input = string.lower(tostring(read()))
-- tostring is invoked on the off chance that a number is entered;
-- our implementation of the read routine is now robust for the
-- most part :P/>/>
The above way allows you to check all of the options as lowercase because all the string entered is run through and all upper case letters are set to their lower case equivalents:
if input == "add" then
15 posts
Posted 14 November 2012 - 05:49 PM
When you use the condition
or "add" then
The interpreter considers this as: "Is the string "add" nil? It's not nil? Oh, then this condition is true. Let's execute the if body!"
If you want to keep your current code you could rewrite it as:
if input == "Add" or input == "add" then
Or…
input = string.lower(tostring(read()))
-- tostring is invoked on the off chance that a number is entered;
-- our implementation of the read routine is now robust for the
-- most part :P/>/>
The above way allows you to check all of the options as lowercase because all the string entered is run through and all upper case letters are set to their lower case equivalents:
if input == "add" then
Thank you so much! You did a very good job of explaining what I was doing wrong and why it was wrong.
4 posts
Location
Aussie :D
Posted 14 November 2012 - 08:17 PM
When you use the condition
or "add" then
The interpreter considers this as: "Is the string "add" nil? It's not nil? Oh, then this condition is true. Let's execute the if body!"
If you want to keep your current code you could rewrite it as:
if input == "Add" or input == "add" then
Or…
input = string.lower(tostring(read()))
-- tostring is invoked on the off chance that a number is entered;
-- our implementation of the read routine is now robust for the
-- most part :P/>/>
The above way allows you to check all of the options as lowercase because all the string entered is run through and all upper case letters are set to their lower case equivalents:
if input == "add" then
I may be wrong, but I don't think read() or io.read() will ever return a number value. Even if only number chars are entered, I believe it will still return a string.
This picture shows type() returning string, from the number 1 being entered into read().
1111 posts
Location
Portland OR
Posted 14 November 2012 - 08:21 PM
You are correct read will return a string. In order for it to be a number you need to wrap it in tonumber()
local input = tonumber(read())
4 posts
Location
Aussie :D
Posted 14 November 2012 - 08:23 PM
When you use the condition
or "add" then
The interpreter considers this as: "Is the string "add" nil? It's not nil? Oh, then this condition is true. Let's execute the if body!"
If you want to keep your current code you could rewrite it as:
if input == "Add" or input == "add" then
Or…
input = string.lower(tostring(read()))
-- tostring is invoked on the off chance that a number is entered;
-- our implementation of the read routine is now robust for the
-- most part :P/>/>
The above way allows you to check all of the options as lowercase because all the string entered is run through and all upper case letters are set to their lower case equivalents:
if input == "add" then
I may be wrong, but I don't think read() or io.read() will ever return a number value. Even if only number chars are entered, I believe it will still return a string.
This picture shows type() returning string, from the number 1 being entered into read().
[attachment=670:2012-11-14_18.12.11.png]
2088 posts
Location
South Africa
Posted 14 November 2012 - 11:07 PM
Yeah, do type(tonumber(read()))…
Spoiler
504 posts
Location
Seattle, WA
Posted 15 November 2012 - 03:42 AM
You're right, that was my mistake; read will only ever return a string value. I don't use read() too often because I have my own routine that allows me to have a little more control over the way input is taken, but I digress. You're right, I'm wrong.