I'm working with python to make a trivia game. Some of my answers are numbers. I'm using the .lower() to make it so that Two or tWO would work, but how would I get 2 to be equivalent in python so that two or 2 would answer the question? I know that C++ has a way to make the two types equivalent, but I'm not sure how to do that with python.
Asked
Active
Viewed 143 times
0
-
1What way does C++ have? – David Robinson Oct 12 '13 at 19:42
-
possible duplicate of [Is there a way to Convert Number words to Integers? Python](http://stackoverflow.com/questions/493174/is-there-a-way-to-convert-number-words-to-integers-python) – David Robinson Oct 12 '13 at 19:43
1 Answers
8
What about this:
answer = raw_input("Your answer: ")
if answer.lower() in ("2", "two"):
# Answer was good.
This method takes the answer, makes it lowercase, and then sees if it can be found in the tuple ("2", "two")
. It will return True
for input such as:
"Two"
"two"
"2"
"tWo"
etc. Anything else will have it return False
.