1,234
edits
Changes
→PART 2 - Arguments
'''Perform the following steps:'''
:#Access your ipython3 shell:<sourcelang="bash">
ipython3
</source>In order to read arguments in Python, we will need to '''import special variables''' from the system. This is a standard 'library' of code provided by the developers of Python. By issuing '''import sys''', you have loaded code written by another person, each 'library' that gets loaded will give us extra functionality in our program. This is done by issuing the import sys function within your ipython3 shell.<br><br>
dir(sys)
</source>You may feel overwhelmed with all of this information, but not to worry, there are additional tools and tips on how to obtain information regarding these functions contain in the library.<br><br>
:#Issue the following functions and note what they do:<sourcelang="python">
print(sys.version) # tells us our python version
print(sys.platform) # tells us our operating system platform
<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:<sourcelang="python">
#!/usr/bin/env python3
IndexError: list index out of range
</source>
:::3. Download the checking 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