How do I check the users input in Python3?
Question:
So my code says this:
a = input("Choose a letter")
if a == "A" or "B" or "D":
cr = "false"
elif a == "C":
cr = "true"
if cr == "false":
print("Sorry you did not asnwer correctly")
import sys
sys.exit()
elif cr == "true":
print("Well done!")
But for some reason, whatever I put, it says it is wrong. Any help? I am new to python and have come from Lua.
Answers:
if a == "A" or "B" or "D":
this evaluates to TRUE always. Because "B" or "D"
always evaluates to TRUE
everytime. So this might boil down to if 0 or 1 or 1
no matter what.
To fix this you need compare after every conditional operator.
if a == "A" or a == "B" or a == "D"
:
if a == "A" or "B" or "D":
cr = "false"
Is wrong. You want:
if a == "A" or a == "B" or a == "D":
cr = "false"
One nice thing you could do is this:
if a in [ 'A', 'B', 'D' ]:
cr = "false"
Or even better rewrite your script to this:
a = input("Choose a letter")
if a == 'C':
print("Well done!")
else:
print("Sorry you did not asnwer correctly")
import sys
sys.exit()
So my code says this:
a = input("Choose a letter")
if a == "A" or "B" or "D":
cr = "false"
elif a == "C":
cr = "true"
if cr == "false":
print("Sorry you did not asnwer correctly")
import sys
sys.exit()
elif cr == "true":
print("Well done!")
But for some reason, whatever I put, it says it is wrong. Any help? I am new to python and have come from Lua.
if a == "A" or "B" or "D":
this evaluates to TRUE always. Because "B" or "D"
always evaluates to TRUE
everytime. So this might boil down to if 0 or 1 or 1
no matter what.
To fix this you need compare after every conditional operator.
if a == "A" or a == "B" or a == "D"
:
if a == "A" or "B" or "D":
cr = "false"
Is wrong. You want:
if a == "A" or a == "B" or a == "D":
cr = "false"
One nice thing you could do is this:
if a in [ 'A', 'B', 'D' ]:
cr = "false"
Or even better rewrite your script to this:
a = input("Choose a letter")
if a == 'C':
print("Well done!")
else:
print("Sorry you did not asnwer correctly")
import sys
sys.exit()