Open main menu

CDOT Wiki β

Changes

OPS435 Python3 Lab 2

447 bytes added, 17:48, 12 September 2019
INVESTIGATION 3: USING LOOP STATEMENTS
= INVESTIGATION 3: USING LOOP STATEMENTS =
:In the first two labsinvestigations, you have been exposed to tools builtin function and methods special list object to write powerful Python scripts. In Lab1Part 1 of the first investigation, this included includes using using variablesinteger and string objects. In Lab Part 2 you learned how to input variables get data from the user by either prompting the user for data using the input () function or get it from the command line using data that are arguments containing the sys.argv list object within a Python script that you run. You also learned about LOGIC control-flow statements in order to make the Python script behave differently based on differing inputdifferent condition.
:You will start to learn about the second major category of control flow statements by learning how to repeat repeatedly executing a command python statement or a series of commandspython statements. Although, you will be learning other scripting techniques, the ability to know how to use variablesdifferent objects, CONDITIONAL and LOOPING control-flow statements will allow you to create useful and powerful programs script to assist you when managing your computer system (including virtual machines).
== PART 1 - Understanding WHILE Loops ==
:'''WHILE loops''' may use a the same type of conditions expression found in if IF statements. While the condition expression is evaluated to True, the code indented under the while loop will be repeated. When the condition expression becomes False the loop will stop repeatingthe indented code.
:'''Perform the following steps'''
:#Create a temporary python file for practicing practising with the followin following examples.:#A '''WHILE''' loop is not the most common type of loop in Python but it's the simplest. Below is a WHILE loop which will run five times. Each time the loop is run, it will add one to the integer count variableobject, increasing the variables numbervalue of the count object:<source lang="python">
count = 0
while count != 5:
count = count + 1
print('loop has ended')
</source>Sometimes you know in advance how many times a loop will execute , this is referred as a determinant loop, but often you don't. For example loops are extremely useful for '''error-checking''' in order to prevent incorrect data being accepted and causing the script not to perform correctly.<br>
:#Here is an example of a loop used for error-checking. Run this code and type several incorrect passwords then the correct one to see what happens:<source lang="python">
password = ''
:::*The script should have a Shebang line
:::*The script should use a variable an integer object named timer with a value of 10
:::*The script should have a while loop that repeats until timer equals 0
:::*The script should print the EXACT OUTPUT as shown
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 lab2e
</source>
== PART 2 - Using WHILE loops with script arguments ==
:You will now learn to make your Python scripts more flexible by using getting numbers as arguments to be used with WHILE loopsin your script. You will learn that all command line arguments used in Python scripts captured by using the special object sys.argv are all strings (not numbers) and therefore. Even the item provided at the command line is a pure number, it cannot be used in Mathematical operations unless they are it is converted into numbers (like an integer)object. You will be learning how to use the int() builtin function in order to convert a numeric string object into an integerobject.
:'''Perform the Following Steps:'''
:#Make a copy of '''lab2e.py''' and call it '''lab2f.py'''.
:#Modify '''lab2f.py''' to change the initial value of the variable '''count''' to the first command line argument when running your Python script. '''WARNING:''' When using arguments as numbers/integers or performing math on arguments you must wrap them in the int() function, for example: '''count = int(sys.argv[1])'''
:::'''Additional Input / Output / Processing Requirements'''
:::*The script should have a '''Shebang line'''
:::*The script should '''import sys'''
:::*The script should use a variable named timer with assign the value of '''int(sys.argv[1])''' to an object named timer
:::*The script should have a while loop that repeats until timer equals 0
:::*The script should print the EXACT OUTPUT as shown
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 lab2f
</source>
== PART 3 - Combining WHILE loops with IF statements==
:Let's improve upon your previous shell python script to further prevent errors from incorrect command line input. You can combine LOGIC control-flow statements with other LOGIC control-flow statements for more complex programming. For example, if you ran the previous Python script without an argument (i.e. empty string), you would encounter an error since it could not convert an empty string to an integer.
:''' Perform the Following Steps '''
:::*The script should have a '''Shebang line'''
:::*The script should '''import sys'''
:::*The script should use a variable named assign the value of '''timer3''' with the value of to an object named '''3timer''' if when there is '''no arguments''' are entered provided. :::*The script should use a variable named timer with assign the value of '''int(sys.argv[1])''' if an object named '''argumentstimer''' when one command line argument (sys.argv[1]) are entered
:::*The script should have a WHILE loop that repeats until (and not including when) timer equals 0
:::*The script should print the EXACT OUTPUT as shown
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 lab2g
</source>
:::4. Before proceeding, make certain that you identify any and all errors in '''lab2flab2g.py'''. When the check script tells you everything is '''OK''', you may proceed to the next step.
<br><br>
1,760
edits