use statement True and False in Python 2.7

command-prompt, if-statement, python

Solution

The return value from `raw_input` is a string and not a boolean. Hence your `is not True` and `is not False` tests, although they have well-defined meaning, that meaning is not the meaning that you intend. You need to compare `HEADER` against string values.

So you would need, for example, code like this:

if HEADER.lower() == 'true':

I used `tolower()` to effect case-insensitive comparison. You may also want to strip off white space:

if HEADER.strip().lower() == 'true':

I'm sure you can fill in the test against `false` yourself.

Even if you did have a boolean, you should not use code like `is not True` or `is False`. You should test for truth with:

if somebool:

or

if not somebool:

because it is much more readable.

Problem

i wish to use a statement "`True`" and "`False`" for my Python (2.7) command prompt ``` segmentation_accuracy(reference=REFERENCE, segmented=SEGMENTED, output=OUTPUT, method=METHOD, threshold=THRESHOLD, sep=SEP, header=HEADER) ``` if header is `True` print a text file with an header, if header is `False` print a text file without an header. in Command Prompt: ``` REFERENCE = raw_input("Reference (*.shp):") SEGMENTED = raw_input("Segmented (*.shp):") METHOD = raw_input("Method (ke, pu, clinton):") if METHOD != "ke" and METHOD != "pu" and METHOD != "clinton": raise ValueError("%s is not a valid method" % METHOD) if METHOD == "ke" or METHOD == "clinton": THRESHOLD = input("Threshold (0.0 - 1.0):") if not check_threshold(THRESHOLD): raise AccuracyException("Threshold of %s is not valid" % THRESHOLD) else: THRESHOLD = None SEP = raw_input("Sep:") HEADER = raw_input("Header (True/False):") if HEADER is not True or HEADER is not False: raise ValueError("%s is not valid" % HEADER) # output OUTPUT = raw_input("Output (*.txt):") ``` when i run the command prompt in windows if i set `raw_input("Header (True/False):")` `True` or `False`, I always get the `ValueError` i also used the combination ``` if HEADER != True or HEADER != False: raise ValueError("%s is not valid" % HEADER) ``` with the same problem

Original source