-2

This is all about accepting input from user and searching with that particular text.

Playing with string.gsub.

io.write("ENTER ANY STORY :-D ")
story=io.read()
io.write("\t OKAY!, THAT'S NICE :-D  ")
io.write("\t DO YOU WANT TO REPLACE ANY TEXT:?  ")
accept=io.read()
if accept=="YES" or "yes" then
  io.write("\t WHICH TEXT TO REPLAE?  ")
  replace=io.read()
  --HERE IS THE REPLACING TEXT
  io.write("\t WITH WHAT:?   ")
  with=io.read()
  result=string.gsub(story,replace,with)
  print("\t THE REPLACED TEXT IS: ",result)
elseif accept=="NO" or "no" then
  print(result)
end

Bug: The elseif loop isn't working!!

Nick Gammon
  • 1,173
  • 10
  • 22

2 Answers2

1

== and or work like mathematical operators in that they are evaluated one at a time, with the == being evaluated first. If accept is 'no', accept=="YES" or "yes" will be evaluated like this:

(accept == "YES") or "yes"
('no' == "YES") or "yes"
false or "yes"
"yes"

In Lua, all values except nil and false are truthy, so your if block will always run instead of your elseif block.

As said in the comments, accept:upper()=="YES" will fix it. accept:upper() returns a string where all the letters of accept are converted to upper case, so then you only have to compare it to one value.

luther
  • 5,195
  • 1
  • 14
  • 24
  • When i wanted to accept input from user in number I always needs to add ("*n")? accept=io.read("*n") why? – Ben Druno May 06 '18 at 16:19
  • That's getting into stuff that's unrelated to the current question. The doc for `io.read` is [here](https://www.lua.org/manual/5.3/manual.html#pdf-file:read). Depending on how much Lua you already know, I recommend searching for a Lua tutorial. Then look up stuff in the [Lua docs](https://www.lua.org/manual/5.3/) for stuff you're not sure about. If you're still having trouble after that, you can ask another question. – luther May 06 '18 at 16:29
  • 1
    Anyway,thanks a lot LUTHER. Your answer helped a lot! – Ben Druno May 06 '18 at 16:31
  • 1
    In case it's not obvious, you'll have the same problem with `accept=="NO" or "no"`. – Nick Gammon May 11 '18 at 03:26
0

Try this..

io.write("ENTER ANY STORY :-D ")
story=io.read()
io.write("\t OKAY!, THAT'S NICE :-D  ")
io.write("\t DO YOU WANT TO REPLACE ANY TEXT:?  ")
accept=io.read()
if accept=="YES" or accept == "yes" then
  io.write("\t WHICH TEXT TO REPLAE?  ")
  replace=io.read()
  --HERE IS THE REPLACING TEXT
  io.write("\t WITH WHAT:?   ")
  with=io.read()
  result=string.gsub(story,replace,with)
  print("\t THE REPLACED TEXT IS: ",result)
elseif accept=="NO" or accept == "no" then
  print(result)
end