[Tutor] Function problem

Kent Johnson kent_johnson at skillsoft.com
Sat Nov 6 14:03:26 CET 2004


At 12:26 PM 11/6/2004 +0300, Eri Mendz wrote:
>I also want to use regular expression for the Y|N portion; e.g.,
>something like if ask == [Y|y|ye|yes] or ask == [N|n|no|nope]... to that
>effect. Any help is appreciated.

import re
yes = re.compile('y|ye|yes', re.IGNORECASE)
if yes.match(ask):
         ...

This will also match 'you' and 'yesterday', if you want to be stricter 
change the RE to '(y|ye|yes)$'

By the way you might want to break out the handlers for C and F into 
separate functions, that would make your main loop more readable. And if 
you convert choice to upper or lower case (e.g. choice = choice.upper()) 
then you only have to test against one style of each character.

Kent



More information about the Tutor mailing list