0

I have a simple input validation code asking the user for either ACT or SAT.

test=input("Are you taking SAT or ACT?..")
while test!=("SAT" or "ACT"):
    print("error")
    test=input("Are you taking SAT or ACT?..")

It seems to work correctly for "SAT" which is in front on line 2, but not ACT! When I type in ACT in the module it will print "error" like it was false. What is my flaw here? Logic? Syntax? Semantic? What can I do to fix it?

Thank you.

3 Answers 3

1

I don't think that this statement is valid:

test!=("SAT" or "ACT")

You may use

test != "SAT" and test != "ACT"

Or use in operator:

test=input("Are you taking SAT or ACT?..")
while test not in ("SAT", "ACT"):
    print("error")
    test=input("Are you taking SAT or ACT?..")
Sign up to request clarification or add additional context in comments.

4 Comments

The statement is perfectly valid, but it does not yield the expected results. Operators in programming languages work in a very simple, well-defined way. On the other hand this means, that they cannot be used like in conversation language. Compare for example this pseudocode "if test is not a or b" (conversational) to "if (test is not a) and (test is not b)" (strict).
Yes. You are right. I meant by "not valid" that the statement doesn't mean anything useful here, because it doesn't achieve what OP wanted, and because "SAT" or "ACT" always yield the same thing, "SAT".
thank you, I get it now-- no matter ACT or SAT, the while loop will always be true when I use "or".
@PupWatthanawong In fact, when you enter "SAT" the condition of the while loop will evaluate to False, because "SAT" or "ACT" always evaluates to "SAT".
1

The expression ("SAT" or "ACT") evaluates to "SAT" since it basically evaluates the OR operation on two strings. In order to fix the issue, this is what you can do:

while(1):
    test = input("Are you taking SAT or ACT")
    if test in ("SAT", "ACT"):
        break

    print("Error")

1 Comment

Using the not in operator would make for a smaller change. Also, no need for parenthesis in a while True:.
0

Teverse the condition from test!=("SAT" or "ACT") to test==("SAT" or "ACT") or you can use IN as explained by Kshitij Saraogi.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.