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

Elseif problem

Started by Thefdjurt, 25 May 2013 - 11:27 AM
Thefdjurt #1
Posted 25 May 2013 - 01:27 PM
I'm working on a calculator where, currently, you have two options: Add and Subtract.

option = read()
if option == "Add" or "add" then
shell.run("add")
elseif option == "Subtract" or "subtract" or "sub" then
shell.run("sub")
else
print("ERROR UNKNOWN PROGRAM!")
end

The add program is working, but when I type Subtract, subtract, or sub , it runs the add program, help :(/>
Lyqyd #2
Posted 25 May 2013 - 03:22 PM
Split into new topic.

If you read your if statement out loud, it would be "If option is equal to "Add", or "add" is not nil or false, then run the add program". It should be "If option is equal to "Add", or option is equal to "add", then run the add program". The second one looks like this:


if option == "Add" or option == "add" then

But really, you're doing it the hard way. You can just look at the first three letters and force them to be lowercase, which would make it a lot easier.


if string.lower(string.sub(option, 1, 3)) == "add" then
  shell.run("add")
elseif string.lower(string.sub(option, 1, 3)) == "sub" then
  shell.run("sub")
end
Thefdjurt #3
Posted 25 May 2013 - 06:08 PM
Split into new topic.

If you read your if statement out loud, it would be "If option is equal to "Add", or "add" is not nil or false, then run the add program". It should be "If option is equal to "Add", or option is equal to "add", then run the add program". The second one looks like this:


if option == "Add" or option == "add" then

But really, you're doing it the hard way. You can just look at the first three letters and force them to be lowercase, which would make it a lot easier.


if string.lower(string.sub(option, 1, 3)) == "add" then
  shell.run("add")
elseif string.lower(string.sub(option, 1, 3)) == "sub" then
  shell.run("sub")
end

Umm thanks, but that didn't work here's the whole code

print("Options: ")
print(">Add")
print(">Subtract")
print("What would you like to do: ")

choice = read()

if choice == "Add" or "add" then --I understand just writing the original code
shell.run("add")

elseif choice == "Subtract" or "subtract" or "sub" then
shell.run("sub")

else print("ERROR NOT AN OPTION")

end

please can you tell me what the error is?
W00dyR #4
Posted 25 May 2013 - 06:46 PM
please can you tell me what the error is?

Lyqyd answered that already, it has to be


if option == "add" or option == "Add" then

Because in Lua you have to declare what it has to be equal to all the time, it will only compare one thing pretty much.
Thefdjurt #5
Posted 25 May 2013 - 07:06 PM
please can you tell me what the error is?

Lyqyd answered that already, it has to be


if option == "add" or option == "Add" then

Because in Lua you have to declare what it has to be equal to all the time, it will only compare one thing pretty much.

I already tried that, and it still didn't work.
Engineer #6
Posted 25 May 2013 - 08:37 PM
Okay, what Lyqyd told you, is that you have to repeat the value:

if something == "test" or something == "something_else" then

So this in your code would be (I also formatted an re-indented it):

print("Options: ")
print(">Add")
print(">Subtract")
print("What would you like to do: ")

local choice = read() --# Localizing is in general better
if choice == "Add" or choice == "add" then 
  shell.run("add")
elseif choice == "Subtract" or choice == "subtract" or choice == "sub" then
  shell.run("sub")
else 
  print("ERROR NOT AN OPTION")
end

But in this case, I would say changing the input to a lowered input: (eg: SomEThing -> something)
This can be achieved by this function: string.lower( s )
Where s is your string.

So you would change your
 local choice = read() 
to:

local choice = string.lower(read())
or

local choice = read():lower()
It both does the same
Then it comes to this, because you can eliminate certain pieces (also included Lyqyd's part):

print("Options: ")
print(">Add")
print(">Subtract")
print("What would you like to do: ")

local choice = read():lower():sub(1, 3)

if choice == "add" or choice == "sub" then
  shell.run(choice)
else 
  print("ERROR NOT AN OPTION")
end
Also compacted it.(You can compact it another 3 lines if you really want to. I dont count whitespace as compacting. And with compacting I also mean its still readable :P/> )

I hope I helped
Edited on 25 May 2013 - 06:46 PM
Thefdjurt #7
Posted 25 May 2013 - 10:28 PM
I hope I helped
Thanks man you rock, I'm pretty knew to programming, and you really simplified what I was supposed to do :D/>/>
+1