newbie question

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Jun 22 23:08:25 EDT 2013


On Sat, 22 Jun 2013 19:39:30 -0700, christhecomic wrote:

> Writing simple program asking a question with the answer being
> "yes"...how do I allow the correct answer if user types Yes, yes, or
> YES?

Take the user's input, strip off any whitespace from the beginning and 
end, then fold the case to a consistent known form, e.g. lowercase.

# in Python 3.x, use input() rather than raw_input()
result = raw_input("Answer my question! ")
result = result.strip().lower()
if result == "yes":
    ...


For English words, like "yes", converting to lowercase will do the job. 
But if you are using Python 3.3 or better, then I recommend you use 
casefold() instead of lower() since that is smarter about converting case 
when you have non-English characters.


-- 
Steven



More information about the Python-list mailing list