Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 2

78 bytes added, 09:07, 26 May 2017
PART 1 - Using IF Statements
<blockquote style="margin-left:35px;">{{Admon/important|style="padding-left:25px"|Number of Arguments with len(sys.argv)|If you are running the '''len(sys.argv)''' function in ipython3, the number of arguments returned will be always be '1'. This number will always be one higher than the actual number of arguments entered, since it also counts the script name as a argument.}}</blockquote>
 
''' Practice Using '''
:#Now it's time to create a new script. Make a copy of '''lab2c.py''' and call it '''lab2d.py'''. Now modify '''lab2d.py''', add a if statement right before your print statements. This if statement should make sure that lab2d.py is using '2' additional arguments.
''' Practice Using IF Statements'''
:'''Perform the following Instructions:'''
:#Make a copy of '''lab2c.py''' and call it '''lab2d.py'''.:#Modify '''lab2d.py''' by adding an if statement immediately BEFORE your print statements. This if statement should make sure that lab2d.py is using '2' additional arguments.
::*The script should have a '''Shebang line'''
::*The script should '''import sys'''::*The script should print a '''usage message ''' IF additional arguments are not givenNOT provided when running script::*The script should '''exit IF ''' (or 'abort') if two additional arguments are not givenprovided when running script::*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:'''<source>
run lab2d.py Jon 20
Hi Jon, you are 20 years old.
</source>
:::'''Sample run 2:'''<source>
run lab2d.py Jen 25
Hi Jen, you are 25 years old.
</source>
:::'''Sample run 3:'''<source>
run lab2d.py
Usage: lab2d.py [name] [age]
</source>
:::'''Sample run 4:'''<source>
run lab2d.py Jon is 20
Usage: lab2d.py [name] [age]
</source>
:::23. Download the check checking script and check your work. Enter the following commands from the bash shell.<source>
cd ~/ops435/lab2/
pwd #confirm that you are in the right directory
</source>
:::34. Before proceeding, make certain that you identify any and all errors in "'''lab2d.py"'''. When the check script tells you everything is "ok"'''OK''', you may procede to the next step.
== PART 2 - Using IF/ELIF/ELSE Statements ==
13,420
edits