[Tutor] 'if a==b or a==c or a==d' alternative (was: pausing a nd layout)

Magnus Lycka magnus@thinkware.se
Wed Jan 8 15:48:02 2003


At 17:37 2003-01-07 +0000, alan.gauld@bt.com wrote:
> > >   if p in 'Qq ':
> > >       sys.exit()
> >
> > I've just looked thru the new stuff on Python 2.3, and it
> > seems that it will support larger subsplices, so that
> >
> > if 'ab' in 'abcdefg':
> >     print 'found'
> >
> > will actually print 'found'. Which  means that the above
> > example would do a sys.exit() if the user types 'Qq'.
>
>Which is probably better than raising an exception which
>is what it would do now... I guess the solution will be
>to put [0] after the test variable if you really only
>want to test 1 char.
>
>if 'ab'[0] in 'abcdefg':...
>
>But that's not so pretty.

Another option is:

"if len(p) == 1 and p in "Qq ":"

or

"if q in list('Qq '):"

But in the case at hand, it seems likely that we know
that q is one character long, for instance if it's the
result of a key press. On the other hand, I fear that
not even all key-presses are one character long...


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se