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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top