Answer a question

I would like to write an argument in an application where the argument that I am calling needs to be referenced on the first iteration/run of the script where the initial_run is set to True. Otherwise this value should be left as false. Now this parameter is configured in a configuration file.

The current code that I written is below. What should be changed in this code to return the True value? Now it only returns the value False.

import sys  
# main
param_1= sys.argv[0:] in (True, False)
print 'initial_run=', param_1

Answers

Running the script from the command line:

# ./my_script.py true

The boolean can be obtined by doing:

import sys

initial_run = sys.argv[1].lower() == 'true'

This way we are doing a comparison of the first argument lowercased to be 'true', and the comparison will return boolean True if the strings match or boolean False if not.

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐