"len(sys.argv) < 2" not satisfied even when command called with only one argument
<pre><code>#! /usr/bin/python3
# pw.py - An insecure password locker program.

PASSWORDS = {'email': 'aklsjdlksajdkljl',
             'blog': 'dklasjkl9379343',
             'luggage': '12345'}

import sys, pyperclip


if len(sys.argv) &lt; 2:
    print('Usage: python ' + sys.argv[0] + ' [' + sys.argv[1] + '] - copy account password')
    sys.exit()

account = sys.argv[1]      # first command line arg is the account name


if account in PASSWORDS:
    pyperclip.copy(PASSWORDS[account])
    print('Password for ' + account + " " + sys.argv[0] + " " + sys.argv[1] + ' copied to clipboard.')
else:
    print('There is no account named ' + account)
</code></pre>

<p>When I type <code>./pw.py email</code> in terminal, it will go straight to the line:  <code>if account in PASSWORDS:</code> and will skip line: <code>if len(sys.argv) &lt; 2:</code></p>

<p>Why did it skip that line?</p>

<p>If <code>sys.argv[1]</code> is defined, the length of <code>sys.argv</code> is 2 or greater. The test <code>sys.argv &lt; 2</code> is only going to be true when <code>sys.argv</code> contains just 0 or 1 items. So the <code>if len(sys.argv) &lt; 2:</code> is not skipped, the test is just <em>false</em> and the associated block is not executed.</p>

<p><code>sys.argv[0]</code> is always set to the script name (here <code>pw.py</code>), so the length would be at least 1.</p>

<p>Note that you just used <code>python pw.py</code> (so no account or other arguments), you'd get an index error, as the following line tries to index to <code>sys.argv[1]</code>, a value that is <em>not set</em>:</p>

<pre><code>print('Usage: python ' + sys.argv[0] + ' [' + sys.argv[1] + '] - copy account password')
# This raises an index error when len(sys.argv) &lt; 2   ^^^
</code></pre>

python