Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 2

341 bytes added, 12:27, 26 May 2017
PART 3 - Using WHILE Loops With IF Statements
== PART 3 - Using WHILE Loops With IF Statements==
: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. '' Practice Using Loops With IF Statements ' Perform the Following Steps ''' :#Now lets make the same script EVEN more intelligent. Make a copy of '''lab2f.py''' and call it '''lab2g.py'''. Now modify :#Modify '''lab2g.py''', add a if 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.
:::'''WARNING: When using arguments as numbers/integers or performing math on arguments you must wrap them in the int() function: number = int(sys.argv[1])'''
:::'''WARNING: Remember to check the number of arguments using len(sys.argv) in a if IF statement'''
::*The script should have a Shebang line
13,420
edits