Pup Watthanawong
Pup Watthanawong

Reputation: 5

Python input validation?

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.

Upvotes: 0

Views: 116

Answers (3)

iways
iways

Reputation: 11

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

Upvotes: 0

Kshitij Saraogi
Kshitij Saraogi

Reputation: 7609

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")

Upvotes: 1

Ammar Alyousfi
Ammar Alyousfi

Reputation: 4372

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?..")

Upvotes: 1

Related Questions