So I feel rather foolish for this, but I haven't used command line often. I'm using sys.argv to gather arguments from the command line. I'm saving one in particular to a variable called test. It is suppose to be a boolean; however, passing in True or False still renders if(test): to be true. It's something simple i'm sure. Ideas?

7 Years
Discussion Span
Last Post by poeticinsanity

sys.argv provides you the string representation of cmd line params. 'True' and 'False' are both strings, and if a string has any content in it at all, the boolean representation is True. You should simply change your condition to if test.lower() == 'true': HTH


There's no straight forward way to convert the strings 'True' and 'False' to boolean True or False, so you're best bet is probably:

if test.lower() == 'true':
    test = True
elif test.lower() == 'false':
    test = False

You could shorten those conditionals even further by only taking into account the first letter:

if test.lower()[0] == 't':
    test = True
elif test.lower()[0] == 'f':
    test = False

Considering you don't specifically need to validate the second case, you could simply use else: , and going even further you could simplify the assignment into a single line like so:

test = [False, True][test.lower()[0] == 't']
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.