Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 2

29 bytes added, 13:17, 1 August 2017
PART 3 - Using WHILE Loops With IF Statements
:::3. Before proceeding, make certain that you identify any and all errors in '''lab2e.py'''. When the check script tells you everything is '''OK''', you may proceed to the next step.
== PART 2 - Using WHILE Loops loops with script Arguments arguments ==
:You will now learn to make your Python scripts more flexible by using numbers as arguments to be used with WHILE loops. You will learn that arguments used in Python scripts are strings (not numbers) and therefore, cannot be used in Mathematical operations unless they are converted into numbers (like an integer). You will be learning how to use the int() function in order to convert a string into an integer.
:'''Perform the Following Steps:'''
 
:#Make a copy of '''lab2e.py''' and call it '''lab2f.py'''.
:::4. 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 proceed to the next step.
== PART 3 - Using Combining WHILE Loops With loops with IF Statementsstatements==
:Let's improve upon your previous shell script to further prevent errors from incorrect input. You can combine LOGIC control-flow statements with 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.
:#Make a copy of '''lab2f.py''' and call it '''lab2g.py'''.
:#Modify '''lab2g.py''', add a an IF statement to the script that checks to see if a argument was entered. If a argument was entered use that number for the timer, if no argument was entered, then by default, the timer should equal 3.
<blockquote style="margin-left:35px;">{{Admon/important|style="padding-left:25px"|ADDITIONAL WARNINGS|When using arguments as numbers/integers or performing math on arguments you must wrap them in the '''int()''' function:<br><source>timer = int(sys.argv[1])</source>Remember to check the number of arguments in an IF statement using:<source>len(sys.argv)</source>}}</blockquote>
:::*The script should use a variable named '''timer''' with the value of '''3''' if '''no arguments''' are entered
:::*The script should use a variable named timer with the value of '''int(sys.argv[1])''' if '''arguments''' 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