Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 2

112 bytes added, 02:35, 25 May 2017
PART 2 - ARGUMENTS
== PART 2 - ARGUMENTS ==
A argument is a value that is passed to a program or passed to a function. In the previous section you passed a argument to the input() function. In this section we will go over the steps of passing a argument to your script, but this time the argument will be passed when we execute your script from the bash shell.
 
'''Perform the following steps
:#In order to read arguments in Python, we will need to import special variables from the system. This is done very easily.<source>
import sys
</source>
 
:#This is a standard 'library' of code provided by the developers of Python. By typing 'import sys' we have loaded code written by another person, each 'library' that gets loaded will give us extra functionality in our program.
 
:#To inspect this library and look at all that it contains we can use the 'dir()' function. This can be used to inspect any library(and more) looking at all the functions and values contained within.<source>
dir(sys)
</source>
 
:#Now don't feel overwhelmed, this will provide a lot of information, but we can also get some hints at the different information this library provides. <source>
print(sys.version) # tells us our python version
sys.exit() # will immediately end our script when run
</source>
 
:#The 'sys.exit()' will immediately end the script on the line it is called, no other line in the script will ever be run. This function will be useful later in this lab, make sure you write it down in your notes.
  :#Create a new script '~/ops435/lab2/showargs.py' and place the following content inside<source>
#!/usr/bin/env python3
print('Print out ALL script arguments: ', arguments)
 
print('Print out the script name: ' + name)
</source>
 :#Run the script and examine the output: <presource>
run showargs.py
run showargs.py testing different arguments
run showargs.py argument1 argument2 argument3
</presource:#The 'sys.argv' contains all arguments, 'sys.argv[0]' contains the name of the script/program, 'sys.argv[1]' contains the first argument, 'sys.argv[2]' contains the second argument. ''' lab2c '''Now it's time to create a new script. Make a copy of '''lab2b.py''' and call it '''lab2c.py'''. Now modify '''lab2c.py''' so that it uses 'sys.argv[1]' and 'sys.argv[2]' instead of the input() functions. Example output is shown below: The script should have a Shebang lineThe script should import sysThe script should use a variable called "name"The script should use a variable called "age"The script should use sys.argv[1] (first argument)The script should use sys.argv[2] (second argument)The script should store the values in the correct variablesThe script should print the EXACT OUTPUT as shown
Sample run ''' Practice Using Arguments ''':#Now it's time to create a new script. Make a copy of '''lab2b.py''' and call it '''lab2c.py'''. Now modify '''lab2c.py''' so that it uses 'sys.argv[1]' and 'sys.argv[2]' instead of the input() functions. Example output is shown below:
::*The script should have a Shebang line::*The script should import sys::*The script should use a variable called "name"::*The script should use a variable called "age"::*The script should use sys.argv[1] (first argument)::*The script should use sys.argv[2] (second argument)::*The script should store the values in the correct variables::*The script should print the EXACT OUTPUT as shown:::Sample run 1:<presource>
run lab2c.py Jon 20
Hi Jon, you are 20 years old.
</presource:::Sample run 2: <presource>
run lab2c.py Jen 25
Hi Jen, you are 25 years old.
</presource::2. This final sample run will give us a error. This is important to see this, as we will fix it in the next section. This error happens if you run the script without any arguments. Since we still try to use them. Lets move forward and leave this error, until we have the right tools to fix it. :::Sample run 3: <presource>
run lab2c.py
---------------------------------------------------------------------------
IndexError: list index out of range
</presource>  ::3. Download the check script and check your work. Enter the following commands from the bash shell.<presource>
cd ~/ops435/lab2/
pwd #confirm that you are in the right directory
ls CheckLab2.py || wget matrix.senecac.on.ca/~acoatley-willis/CheckLab2.py
python3 ./CheckLab2.py -f -v lab2c
</presource::4. Before proceeding, make certain that you identify any and all errors in "lab2c.py". When the check script tells you everything is "ok", you may procede to the next step.
= INVESTIGATION 2: IF STATEMENTS =
198
edits