Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>let's say on the command-line you have:</p> <pre><code>C:\&gt; C:\Documents and Settings\fred\My Documents\Downloads\google-python-exercises \google-python-exercises\hello.py John </code></pre> <p>to make it easier to read, let's just shorten this to:</p> <pre><code>C:\&gt; hello.py John </code></pre> <p><code>argv</code> represents all the items that come along via the command-line input, but counting starts at zero (0) not one (1): in this case, "<code>hello.py</code>" is element 0, "<code>John</code>" is element 1</p> <p>in other words, <code>sys.argv[0] == 'hello.py'</code> and <code>sys.argv[1] == 'John'</code> ... but look, how many elements is this? 2, right! so even though the numbers are 0 and 1, there are 2 elements here.</p> <p><code>len(sys.argv) &gt;= 2</code> just checks whether you entered <em>at least</em> two elements. in this case, we entered exactly 2.</p> <p>now let's translate your code into English:</p> <pre><code>define main() function: if there are at least 2 elements on the cmd-line: set 'name' to the second element located at index 1, e.g., John otherwise there is only 1 element... the program name, e.g., hello.py: set 'name' to "World" (since we did not get any useful user input) display 'Hello' followed by whatever i assigned to 'name' </code></pre> <p>so what does this mean? it means that if you enter:</p> <ul> <li>"<code>hello.py</code>", the code outputs "<code>Hello World</code>" because you didn't give a name</li> <li>"<code>hello.py John</code>", the code outputs "<code>Hello John</code>" because you did</li> <li>"<code>hello.py John Paul</code>", the code still outputs "<code>Hello John</code>" because it does not save nor use <code>sys.argv[2]</code>, which was "<code>Paul</code>" -- can you see in this case that <code>len(sys.argv) == 3</code> because there are 3 elements in the <code>sys.argv</code> list?</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload