<p>How do I have a Python script that a) can accept user input and how do I make it b) read in arguments if run from the command line?</p>

[{'answer_id': 70814, 'author': 'lbz', 'author_id': 11530, 'author_profile': 'https://Stackoverflow.com/users/11530', 'pm_score': 9, 'selected': False, 'text': '<pre><code>var = raw_input("Please enter something: ")\nprint "you entered", var\n</code></pre>\n\n<p>Or for Python 3:</p>\n\n<pre><code>var = input("Please enter something: ")\nprint("You entered: " + var)\n</code></pre>\n'}, {'answer_id': 70818, 'author': 'David Webb', 'author_id': 3171, 'author_profile': 'https://Stackoverflow.com/users/3171', 'pm_score': 5, 'selected': False, 'text': '<p>The best way to process command line arguments is the <a href="https://docs.python.org/library/argparse.html" rel="noreferrer"><code>argparse</code> module.</a></p>\n\n<p>Use <code>raw_input()</code> to get user input.  If you import the <a href="https://docs.python.org/library/readline.html" rel="noreferrer"><code>readline module</code></a> your users will have line editing and history.</p>\n'}, {'answer_id': 70833, 'author': 'Antti Rasinen', 'author_id': 8570, 'author_profile': 'https://Stackoverflow.com/users/8570', 'pm_score': 10, 'selected': True, 'text': '<p>To read user input you can try <a href="https://docs.python.org/dev/library/cmd.html" rel="noreferrer">the <code>cmd</code> module</a> for easily creating a mini-command line interpreter (with help texts and autocompletion) and <a href="https://docs.python.org/2/library/functions.html#raw_input" rel="noreferrer"><code>raw_input</code></a> (<a href="https://docs.python.org/dev/library/functions.html#input" rel="noreferrer"><code>input</code></a> for Python 3+) for reading a line of text from the user.</p>\n\n<pre><code>text = raw_input("prompt")  # Python 2\ntext = input("prompt")  # Python 3\n</code></pre>\n\n<p>Command line inputs are in <code>sys.argv</code>. Try this in your script:</p>\n\n<pre><code>import sys\nprint (sys.argv)\n</code></pre>\n\n<p>There are two modules for parsing command line options: <a href="https://docs.python.org/dev/library/optparse.html" rel="noreferrer"><s><code>optparse</code></s></a> (deprecated since Python 2.7, use <a href="https://docs.python.org/dev/library/argparse.html" rel="noreferrer"><code>argparse</code></a> instead) and <a href="https://docs.python.org/dev/library/getopt.html" rel="noreferrer"><code>getopt</code></a>. If you just want to input files to your script, behold the power of <a href="https://docs.python.org/dev/library/fileinput.html" rel="noreferrer"><code>fileinput</code></a>.</p>\n\n<p>The <a href="https://docs.python.org/dev/library/" rel="noreferrer">Python library reference</a> is your friend.</p>\n'}, {'answer_id': 70841, 'author': 'Simon Peverett', 'author_id': 6063, 'author_profile': 'https://Stackoverflow.com/users/6063', 'pm_score': 4, 'selected': False, 'text': '<p>Use \'raw_input\' for input from a console/terminal.</p>\n\n<p>if you just want a command line argument like a file name or something e.g. </p>\n\n<pre><code>$ python my_prog.py file_name.txt\n</code></pre>\n\n<p>then you can use sys.argv...</p>\n\n<pre><code>import sys\nprint sys.argv\n</code></pre>\n\n<p>sys.argv is a list where 0 is the program name, so in the above example sys.argv[1] would be "file_name.txt"</p>\n\n<p>If you want to have full on command line options use the optparse module.</p>\n\n<p>Pev</p>\n'}, {'answer_id': 70869, 'author': 'Vhaerun', 'author_id': 11234, 'author_profile': 'https://Stackoverflow.com/users/11234', 'pm_score': 4, 'selected': False, 'text': "<p>Careful not to use the <code>input</code> function, unless you know what you're doing. Unlike <code>raw_input</code>, <code>input</code> will accept any python expression, so it's kinda like <code>eval</code></p>\n"}, {'answer_id': 3427325, 'author': 'GreenMatt', 'author_id': 197011, 'author_profile': 'https://Stackoverflow.com/users/197011', 'pm_score': 3, 'selected': False, 'text': '<p>As of Python <del>3.2</del> 2.7, there is now <a href="http://docs.python.org/dev/library/argparse.html" rel="noreferrer">argparse</a> for processing command line arguments.</p>\n'}, {'answer_id': 8334188, 'author': 'steampowered', 'author_id': 404699, 'author_profile': 'https://Stackoverflow.com/users/404699', 'pm_score': 8, 'selected': False, 'text': '<p><code>raw_input</code> is no longer available in Python 3.x.  But <code>raw_input</code> was renamed <code>input</code>, so the same functionality exists.</p>\n\n<pre><code>input_var = input("Enter something: ")\nprint ("you entered " + input_var) \n</code></pre>\n\n<p><a href="http://docs.python.org/py3k/whatsnew/3.0.html#builtins" rel="noreferrer">Documentation of the change</a></p>\n'}, {'answer_id': 13089887, 'author': 'Matt Olan', 'author_id': 1776131, 'author_profile': 'https://Stackoverflow.com/users/1776131', 'pm_score': 3, 'selected': False, 'text': '<p>If you are running Python &lt;2.7, you need <a href="http://docs.python.org/library/optparse.html" rel="noreferrer">optparse</a>, which as the doc explains will create an interface to the command line arguments that are called when your application is run.</p>\n\n<p>However, in Python ≥2.7, optparse has been deprecated, and was replaced with the <a href="http://docs.python.org/library/argparse.html" rel="noreferrer">argparse</a> as shown above. A quick example from the docs...</p>\n\n<blockquote>\n  <p>The following code is a Python program that takes a list of integers\n  and produces either the sum or the max:</p>\n</blockquote>\n\n<pre><code>import argparse\n\nparser = argparse.ArgumentParser(description=\'Process some integers.\')\nparser.add_argument(\'integers\', metavar=\'N\', type=int, nargs=\'+\',\n                   help=\'an integer for the accumulator\')\nparser.add_argument(\'--sum\', dest=\'accumulate\', action=\'store_const\',\n                   const=sum, default=max,\n                   help=\'sum the integers (default: find the max)\')\n\nargs = parser.parse_args()\nprint args.accumulate(args.integers)\n</code></pre>\n'}, {'answer_id': 30341035, 'author': 'Viswesn', 'author_id': 527813, 'author_profile': 'https://Stackoverflow.com/users/527813', 'pm_score': 4, 'selected': False, 'text': '<p>This simple program helps you in understanding how to feed the user input from command line and to show help on passing invalid argument.</p>\n\n<pre><code>import argparse\nimport sys\n\ntry:\n     parser = argparse.ArgumentParser()\n     parser.add_argument("square", help="display a square of a given number",\n                type=int)\n    args = parser.parse_args()\n\n    #print the square of user input from cmd line.\n    print args.square**2\n\n    #print all the sys argument passed from cmd line including the program name.\n    print sys.argv\n\n    #print the second argument passed from cmd line; Note it starts from ZERO\n    print sys.argv[1]\nexcept:\n    e = sys.exc_info()[0]\n    print e\n</code></pre>\n\n<p>1) To find the square root of 5</p>\n\n<pre><code>C:\\Users\\Desktop&gt;python -i emp.py 5\n25\n[\'emp.py\', \'5\']\n5\n</code></pre>\n\n<p>2) Passing invalid argument other than number</p>\n\n<pre><code>C:\\Users\\bgh37516\\Desktop&gt;python -i emp.py five\nusage: emp.py [-h] square\nemp.py: error: argument square: invalid int value: \'five\'\n&lt;type \'exceptions.SystemExit\'&gt;\n</code></pre>\n'}, {'answer_id': 42305071, 'author': 'CorpseDead', 'author_id': 5539337, 'author_profile': 'https://Stackoverflow.com/users/5539337', 'pm_score': 3, 'selected': False, 'text': "<p>If it's a 3.x version then just simply use:</p>\n\n<pre><code>variantname = input()\n</code></pre>\n\n<p>For example, you want to input 8:</p>\n\n<pre><code>x = input()\n8\n</code></pre>\n\n<p>x will equal 8 but it's going to be a string except if you define it otherwise.</p>\n\n<p>So you can use the convert command, like:</p>\n\n<pre><code>a = int(x) * 1.1343\nprint(round(a, 2)) # '9.07'\n9.07\n</code></pre>\n"}, {'answer_id': 44314236, 'author': 'Mark', 'author_id': 8075198, 'author_profile': 'https://Stackoverflow.com/users/8075198', 'pm_score': 2, 'selected': False, 'text': "<p>In Python 2:</p>\n\n<pre><code>data = raw_input('Enter something: ')\nprint data\n</code></pre>\n\n<p>In Python 3:</p>\n\n<pre><code>data = input('Enter something: ')\nprint(data)\n</code></pre>\n"}, {'answer_id': 54241008, 'author': 'Will Charlton', 'author_id': 2517989, 'author_profile': 'https://Stackoverflow.com/users/2517989', 'pm_score': 2, 'selected': False, 'text': '<pre><code>import six\n\nif six.PY2:\n    input = raw_input\n\nprint(input("What\'s your name? "))\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/70797', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1384652/']