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
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?..")
4 Comments
"SAT" or "ACT" always yield the same thing, "SAT".while loop will evaluate to False, because "SAT" or "ACT" always evaluates to "SAT".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
not in operator would make for a smaller change. Also, no need for parenthesis in a while True:.Teverse the condition from test!=("SAT" or "ACT") to test==("SAT" or "ACT") or you can use IN as explained by Kshitij Saraogi.