1,234
edits
Changes
→PART 2 - Arguments
print(sys.argv) # tells us our arguments or shell version if issued from shell
sys.exit() # will immediately end the running Python script, ignoring the remaining lines in the Python script
</source>Note that the '''sys.exit()''' function will be useful later in this lab, make sure you write it down in your notes.<br><br>Instead of using the '''input()''' function to prompt the user for data, we can use the '''sys.argv''' function to store data as a result of running the Python script with arguments. The '''sys.argv''' function, when used within your Python script can store the following:<ul><li>'''sys.argv''' - stores all argument data</li><li>'''sys.argv[0]''' - stores the name of the script/program</li><li>'''sys.argv[1]''' - stores the first argument</li><li>'''sys.argv[2]''' - stores the second argument</li><li>etc...</li></ul><br>
:#Create a new script called '''~/ops435/lab2/showargs.py''' and add the following content:<source>
#!/usr/bin/env python3
:::4. Before proceeding, make certain that you identify any and all errors in '''lab2c.py'''. When the check script tells you everything is '''OK''', you may procede to the next step.
<br><br>
= INVESTIGATION 2: USING LOGIC STATEMENTS =
:In computer programming, control flow statement can be used to change the direction (flow) of a program. In this section, you will focus on LOGIC control flow statements that are used to change the way each script runs, based entirely on input (either user input, or command arguments). In this section, we will discuss several LOGIC control flow statements including: IF, and IF/ELIF/ELSE.
:#Modify '''lab2g.py''', add a 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>count timer = int(sys.argv[1])</source>Remember to check the number of arguments in an IF statement using:<source>len(sys.argv)</source>}}</blockquote>
:::'''Additional Input / Output / Processing Requirements'''