[Tutor] Fahrenheit to Celsius Conversion another problem and Programming Paradigm
Mats Wichmann
mats at wichmann.us
Wed Jun 14 15:22:51 EDT 2017
On 06/14/2017 12:18 PM, Sibylle Koczian wrote:
> Correct usage would be:
>
> if myvar == val1 or myval == val2:
> or
> if myvar in (val1, val2):
Just piling on here to say I find the second form very useful to collect
arguments in a "friendly" way, if you don't have a reason to very
rigidly constrain them. For example, if you have an on/off type switch
in your arguments (or "input()" type calls), you can say something like
if myarg in ('T', 't', 'True', 'true', 'Y', 'y', 'Yes', 'yes', '1',
'ON', 'On', 'on'):
Since that's getting too long, we can smash the casing:
if myarg.lower() in ('t', 'true', 'y', 'yes', '1', 'on'):
Of course if you do any serious argument handling, it's better to use
something like optparse (and earlier argparse) module so you're not
reinventing a wheel which has been massively worked on already.
More information about the Tutor
mailing list