from sys import argv
Says: From the module named sys (which, if it were in a file would be in a file named 'sys.py') import the thing named 'argv' into the current name context. That makes it possible to refer to 'argv' without having to mention the module it came from. If you had instead said import sys then you would haveall the things in sys available, but you would have to access them scoped by their module name. For instance script, first, second, third = sys.argv script, first, second, third = argv
Says: unpack into the four named variables, the values contained in the variable named 'argv'. Python allows you do do simultaneous assignments so v_1,v_2 = "one",2 for instance works as if you said both v_1="one" and v_2=2 This is one of the cool syntactical features of Pyrhon. The quoted lineassumes that argv holds exactly 4 values which can be unpacked into the four named variables. In real code, that assumption would be a very bad idea.
print whatever
You are using a 2.x version of Python which has an operator named print. Versions 3.x and above have a function named print. The difference is that operator print prints everything that follows it on the line: print "Hello", "world" whereas function print requires the things to be printed be passed as parameters in a function call: print("Hello","world") There is no urgent reason to choose Python 3.x over Python 2.x. Whichever one you have easy access to is fine.
griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
First, let me recommend to you that you use the [CODE] button. Either click it and then paste your code between the tags, or highlight your code and click it. This is prettier
# Get command line arguments from the sys module
from sys import argv
# ...
than yours, don't you you agree?
Second, for future reference. Note the "mark thread as solved" link near the bottom of the page. Only you, the original poster, can see that link. Using itafter the thread is solved helps keep DaniWeb functioning smoothly.
Finally: Your comments are not quite right. For one thing, no real programmer would over-comment so much, but ignore that: You are indicating your understanding of the code, not "real comment"ing. The issue I still have is that you are skipping over how things are put into sys.argv When I invoke a python script from the command line: python myscript.py some subsequent words the python interpreter puts the script name into sys.argv[0] and any subsequent arguments into the rest of the sys.argv list. So from the command line above, you would end up with sys.argv == ['myscript.py', 'some', 'subsequent', 'words'] If you don't put exactly three arguments on the console command line when invoking your script, you will get a failure. Your comment almost seems to imply that the program interacts with the user to get the values. Not true.
griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256
Hmm. I thought I was being clear.
If you chant myscript.py with no further arguments, then sys.argv has just one list element: "myscript.py". If you chant myscript.py argumentOne then sys.argv has two list elements: "myscript.py" and "argumentOne" ... and so forth for any number of command line arguments. Your script requires that you invoke it with exactly three arguments. If you were to invoke it with, say, two arguments or four arguments, it would fail. I'm not really sure what part of what I said was a problem. Does this help?
griswolf
Veteran Poster
1,165 posts since Apr 2010
Reputation Points: 344
Solved Threads: 256