if-else-then

Thorsten Kampe thorsten at thorstenkampe.de
Sat Sep 11 15:56:29 CEST 2004


* TuPLaD (2004-09-11 14:34 +0200)

Please don't fullquote.

> if user type: n, no, nop, nope then print 'You canceled'
> else
> if user type: y, ye, yes, yep then print 'We are beginning'
> 
> How do i do that ?

There's a tutor list where people who are constanty bored just wait
for questions like this.

Excerpt from a template.py of mine:

def equivalent_answers(answer):
    """ generate equally valid answers ('y', 'ye', 'yes') """
    return [answer[:index + 1] for index in range(len(answer))]

MyOptions = {'verbose': None}
    
myanswer = raw_input('%s%s%s' % ('verbose',
                                 '? [yes|no|ignore|abort]: '),
                                 [n])).lower()

if   myanswer == '':  # 'empty' answer
    MyOptions['verbose']  = False
   
elif myanswer in equivalent_answers('yes'):
    MyOptions['verbose']  = True

elif myanswer in equivalent_answers('no'):
    MyOptions['verbose'] = False

elif myanswer in equivalent_answers('ignore'):
    pass

elif myanswer in equivalent_answers('abort'):
    pass

else:
    print "ignoring invalid answer: '%s'" % myanswer



More information about the Python-list mailing list