Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 2

424 bytes removed, 13:52, 1 August 2017
PART 1 - Understanding WHILE Loops
:'''Perform the following steps'''
:#Open ipython3<source>Create a temporary python file for practicing with the followin examples.ipython3</source>:#A '''WHILE''' loop can be used to create a '''Determinant Loop'''. That term means that is not the number most common type of loops (also referred to as: loop in Python but it'repetitions' or 'iterations') are known or 'determined' in advances the simplest. Below is a determinant WHILE loop that which will count to 5run five times. Each time the loop is run, it will add one to the count variable, increasing the variables number.<br><br>:#Issue the following at the ipython3 shell to see what happens:<sourcelang="python">
count = 0
while count != 5:
count = count + 1
print('loop has ended')
</source>A WHILE loop can also be used as an '''Indeterminant Loop'''. That term means that the number of loops are '''unknown''' Sometimes you know in advance. For example, the loop keeps on repeating until the user enters the correct data - we have no idea how many times that user a loop will do that, since that is the job of the user running the script and NOT the programmerexecute but often you don't. Indeterminant Loops 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><br>:#Here is an example of an indeterminant a loop used for error-checking.<br> Issue the following at the ipython3 shell Run this code and type several incorrect passwords then the correct one to see what happens:<sourcelang="python">
password = ''
while password != 'P@ssw0rd':
blast off!
</source>
:::2. Download the check script and check your work. Enter the following commands from the bash shell.<sourcelang="bash">
cd ~/ops435/lab2/
pwd #confirm that you are in the right directory