Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 2

137 bytes added, 02:49, 25 May 2017
INVESTIGATION 3: LOOPS
While loops use a the same type of conditions found in if statements. While the condition is True, the code indented under the while loop will be repeated. When the condition becomes False the loop will stop repeating.
''' Understanding While Loops ''':'''Perform the following steps''':#Below is a program that will count to 5. Each time the loop is run, it will add one to the count variable, increasing the variables number.<presource>
count = 0
while count != 5:
count = count + 1
print('loop has ended')
</presource:#Here is an example of guessing the correct password:<presource>
password = ''
while password != 'P@ssw0rd':
password = input("Type in a password: ")
print('Congratulations you guessed the correct password!')
</presource>
''' lab2e Practice Using a While Loop''':#Now it's time to create a new script. Make a new file called '''lab2e.py''', this script will contain a while loop. This while loop will count down from 10, print each value as it counts down. When it gets to the end it will output 'blast off!'. The script should have a Shebang lineThe script should use a variable named timer with a value of 10The script should have a while loop that repeats until timer equals 0The script should print the EXACT OUTPUT as shown
::*The script should have a Shebang line::*The script should use a variable 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:::Sample run:<presource>
run lab2e.py
10
1
blast off!
</presource>
:::2. 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 lab2e
</presource:::3. Before proceeding, make certain that you identify any and all errors in "lab2d.py". When the check script tells you everything is "ok", you may procede to the next step.
== PART 2 - WHILE LOOPS WITH ARGUMENTS ==
Before proceeding, make certain that you identify any and all errors in "lab2d.py". When the check script tells you everything is "ok", you may procede to the next step.
 
 
= LAB 2 SIGN-OFF (SHOW INSTRUCTOR) =
198
edits