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

Favored buy shovel....

Started by SethShadowrider, 22 July 2012 - 02:37 AM
SethShadowrider #1
Posted 22 July 2012 - 04:37 AM
Hello everybody. This may seem like a strange title but it is NOT a sign that i'm crazy. I have been trying to create a shopkeeper program, but no matter what you enter it always makes you buy a shovel. I'm not sure whats wrong with it, I have tried several different ways of doing this but with no results I decided to post my original script online to see what others thought…(maybe you see something I miss?)
Anyways, code:

shell.run("clear")
print("Shopkeeper> What would you like to do today?")
write("Shopper> ")
local BOR = read()
if BOR == "buy" or "Buy" then
print("Shopkeeper> What would you like to buy today?")
write("Shopper> ")
local BD = read()
if BD == "shovel" or "Shovel" then
rs.setOutput("right", true)
sleep(.5)
rs.setOutput("right", false)
print("Shopkeeper> Have a nice day!")
sleep(2)
os.shutdown()
elseif BD == "sword" or "Sword" then
rs.setOutput("left", true)
sleep(.5)
rs.setoutput("left", false)
print("Shopkeeper> Have a nice day!")
sleep(2)
os.shutdown()
end
elseif BOD == "repair" or "Repair" then
print("Shopkeeper> Please put your item in the repair bin")
rs.setOutput("top", true)
sleep(.5)
rs.setOutput("top", false)
print("Shopkeeper> Have a nice day!")
sleep(2)
os.shutdown()
else
print("Shopkeeper> That is not a service.")
sleep(2)
os.reboot()
end
P.S Please note that this code is in early stages and is not actually going to be used yet (pricing and other items not yet implemented)
P.P.S Any efficiency tips also appreciated
Lyqyd #2
Posted 22 July 2012 - 05:38 AM
When you're comparing, the line 'if BD == "shovel" or "Shovel" then' is causing the problem. That line says, "If the value in the variable BD is "shovel", or if the string "Shovel" is not nil or false (which it NEVER will be), then…". To fix it, simply change it (and all lines like it) to:


if BD == "shovel" or BD == "Shovel" then

Or better yet, use:


local BD = tolower(read())
if BD == "shovel" then

Which changes whatever they input to all lowercase before storing it in the BD variable.