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

Program misinterpreting user input

Started by realgogogoluke, 24 May 2014 - 12:47 AM
realgogogoluke #1
Posted 24 May 2014 - 02:47 AM
The following code asks the user a question, then does an action based on the answer. However, the code always defaults to the first choice. Why might that be happening?

os.pullEvent = os.pullEventRaw
print "FactoryOS is loading!"
sleep(3)
print "Worker or boss?"
e = read()
if e == "worker" or "Worker" then
print "Welcome Worker!"
shell.run("chat join Factory Worker"..os.getComputerID())
sleep(3)
os.reboot()
elseif e == "Boss" or e == "boss" then
print "What is the password?"
pass = read("*")
if pass == "password" then
print "Welcome Boss!"
shell.run("chat join Factory Boss"..os.getComputerID())
sleep(3)
os.reboot()
else
print "Rebooting"
sleep(3)
os.reboot()
end
end

EDIT: Found out it does work, it's just the OR command not doing what it should.
Edited on 24 May 2014 - 12:58 AM
Inumel #2
Posted 24 May 2014 - 03:00 AM
The following code asks the user a question, then does an action based on the answer. However, the code always defaults to the first choice. Why might that be happening?

os.pullEvent = os.pullEventRaw
print "FactoryOS is loading!"
sleep(3)
print "Worker or boss?"
e = read()
if e == "worker" or "Worker" then
print "Welcome Worker!"
shell.run("chat join Factory Worker"..os.getComputerID())
sleep(3)
os.reboot()
elseif e == "Boss" or e == "boss" then
print "What is the password?"
pass = read("*")
if pass == "password" then
print "Welcome Boss!"
shell.run("chat join Factory Boss"..os.getComputerID())
sleep(3)
os.reboot()
else
print "Rebooting"
sleep(3)
os.reboot()
end
end

EDIT: Found out it does work, it's just the OR command not doing what it should.

You messed up a bit on the first "or"

if e == "worker" or "Worker" then
should be

if e == "worker" or e == "Worker"
Also you can avoid using or conditionals by setting everything set by the read to lower, or upper


e = string.lower(read())
if e == "worker"

ect
Edited on 24 May 2014 - 01:01 AM
Lyqyd #3
Posted 24 May 2014 - 03:00 AM
No, everything is working as it should. There's no problem with the "or" keyword. You've just forgotten to compare "Worker" to anything, so since that string literal is never nil and it is never false, that condition will always evaluate to true. Add the comparison to e like you have below and it will work fine.

You should really use string.lower, though. Seems easier.
realgogogoluke #4
Posted 24 May 2014 - 03:05 AM
Thanks, will change!