Changes

Jump to: navigation, search

OPS435 Python3 Lab 2

481 bytes added, 12:22, 12 September 2019
PART 2 - Arguments
:::4. Before proceeding, make certain that you identify any and all errors in '''lab2b.py'''. When the check script tells you everything is '''OK''', you may procede to the next step.
== PART 2 - Arguments Command line arguments ==:An A command line argument is a data item that is passed to a program or that can be used within that program. A function argument is also a data item that is passed to a function that can be used for processing within that program or function. In the previous section, you passed an argument to the '''input()''' function. In this section, you will learn how to pass an argument to your Python script, but this time, this argument will be passed when we execute your Python script from the bash shell.
'''Perform the following steps:'''
:#Use a temporary file for testing your work for this section.
:#In order to read arguments in Python, we will need to a special python object called '''sys.argv''' from the '''sys''' module. We can use the python keyword '''import special objects''' from to load the system'''sys''' module so that we can access it in our python script. '''sys. This argv''' is a standard list object which is used to hold everything given at the command line, including the command itself. The '''sys''library' module is one of code provided by the developers of built-in modules come with the Pythoninterpreter. By issuing '''import sys''', you have loaded code written by another person, each 'library' (or 'module') that gets loaded will give us extra functionality and objects to our programpython script.<br><br>
:#Start with the following line to import additional python objects:<source>
import sys
print(sys.version) # tells us the version of the python currently in use
print(sys.platform) # tells us our operating system platform
print(sys.argv) # tells us a list of all items given at the command line when running our arguments or shell version if issued python script from a command shellprint(len(sys.argv)) # tells us the number of command line arguments the user provide issued from a command shell
sys.exit() # will immediately end the running Python script, ignoring the remaining lines in the Python script
</source><br>Instead of using the '''input()''' function to prompt the user for data, we can use the '''sys.argv''' <b>list object</b> to store capture data as a result of running the Python script with arguments. The list object '''sys.argv''', when used within your Python script can store the following:<ul><li>'''sys.argv''' - stores all argument items</li><li>'''sys.argv[0]''' - stores the name of the script/program</li><li>'''sys.argv[1]''' - stores the first argument</li><li>'''sys.argv[2]''' - stores the second argument</li><li>etc...</li><li>'''len(sys.argv)''' - gives the number of arguments</li></ul><br>
:#Create a new script called '''~/ops435/lab2/showargs.py''' and add the following content:<source lang="python">
#!/usr/bin/env python3
''' Practice Using Arguments '''
:Now it's time to create a new script, but unlike prompting the user for data, the data items provided at the command line (as arguments from running your Python script) will be used instead. Refer to variable object name and prompt text information when creating your Python script. Refer to Sample Runs displayed below for exact prompt and output requirements.
:'''Perform the following Instructions:'''
:#Make a copy of '''lab2b.py''' and call it '''lab2c.py'''.
:#Modify '''lab2c.py''' to use the '''sys.argv[1]''' and '''sys.argv[2]''' functions list items instead of the '''input()''' function (used in your previous ''lab2b.py'' script).
:::'''Input / Output Requirements'''
:::*The script should have a '''Shebang line'''
:::*The script should contain '''import sys'''
:::*The script should use a variable string object called '''name''':::*The script should use a variable an integer object called '''age''':::*The script should use '''sys.argv[1]''' (first argument)and assign it to the string object named '''name''':::*The script should use '''sys.argv[2]''' (second argument):::*The script should store and assign it to the values in the correct variables (including case sensitivity)integer object named '''age'''
:::*The script should print the EXACT OUTPUT as shown (including case sensitivity)
cd ~/ops435/lab2/
pwd #confirm that you are in the right directory
ls CheckLab2.py || wget https://rawict.githubusercontentsenecacollege.comca/Seneca-CDOT~raymond.chan/ops435/masterlabs/LabCheckScripts/CheckLab2.py
python3 ./CheckLab2.py -f -v lab2c
</source>
1,760
edits

Navigation menu