質問

I'm new to Lua and I have been having a problem with this bit of code. It is supposed to verify user input with io.read then run the correct if, elseif, or else statement. Once the user puts in the correct response the code should end. For some reason the code will only run the initial if statement. Any help would be greatly appreciated.

repeat
    resp1 = io.read()

    if resp1 == "Yes" or "yes" then
        print("Well alright then, come on in.")
        print("Let me just take your blood sample like all the rest and you'll be good to go.")
    elseif resp1 == "No" or "no" then
        print("Oh, your not? Then why are you up here?")
        print("Oh nevermind then. None of my business anyways. All I'm supposed to do is take a blood sample for everyone who enters the town.")
        print("So let us get that over with now, shall we?")
    else
        print("Please respond with Yes or No")
    end
until resp1 == "Yes" or resp1 == "No" or resp1 == "no" or resp1 == "yes"
役に立ちましたか?

解決

Your issue is with this line:

if resp1 == "Yes" or "yes" then

These are two separate expressions, and in Lua, everything except nil and false are truthy values, so it will pick that if statement to run, because a string in Lua, even an empty one, evaluates to true as far as conditions are concerned. If it helps you understand, think of it like this:

if (resp1 == "Yes") or ("yes") then

If you actually wanted to compare resp1 against two values, you would do it like this:

if resp1 == "Yes" or resp1 == "yes" then

However, this is a simpler solution for what you're trying to achieve:

if resp1:lower() == 'yes' then

In fact, you could clean up your loop, too. Make it more readable. Make use of multi-line strings instead of multiple print calls and break as well.

repeat
    resp1 = io.read():lower() -- read in lowercased input

    if resp1 == 'yes' then
        print[[Well alright then, come on in.
Let me just take your blood sample like all the rest and you'll be good to go.]]
        break -- exit from the loop here
    elseif resp1 == 'no' then
        print[[Oh, your not? Then why are you up here?
Oh nevermind then. None of my business anyways. All I'm supposed to do is take a blood sample for everyone who enters the town.
So let us get that over with now, shall we?]]
        break
    else
        print'Please respond with Yes or No'
    end

until false -- repeat forever until broken

他のヒント

I did that way...

repeat
 print("Please respond with Yes or No.")
    resp1 = io.read()

    if resp1 == "Yes" or resp1 == "yes" then
        print("Well alright then, come on in.")
        print("Let me just take your blood sample like all the rest and you'll be good to go.")

    elseif resp1 == "No" or resp1 == "no" then
        print("Oh, your not? Then why are you up here?")
        print("Oh nevermind then. None of my business anyways. All I'm supposed to do is take a blood sample for everyone who enters the town.")
        print("So let us get that over with now, shall we?")
        else
        print("Please, use just yes or no!!")
     end

   until resp1 == "Yes" or resp1 == "No" and resp1 == "no" or resp1 == "yes"
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top