198
edits
Changes
→INVESTIGATION 1: USER INPUT
print('Hi ' + name + ', you are ' + str(age) + ' years old.')
</source>
:#Try running this script inside ipython3 and study the output:<presource>
run lab2a.py
</presource> :#This Python script is not very useful, it also gives the same output everytime it runs. In ipython3 lets use the input() function to get information from the user. Place a question or hint as a argument in the input() function, this will aid the user in typing in the correct information. <presource>
colour = input("Type in a colour and press enter: ")
Type in a colour and press enter: red
</presource> :#Now print out the variable '''colour''' and it should contain the value you entered after the prompt.<presource>
print(colour)
print('The colour I typed in is: ' + colour)
</presource>
:#If you didn't get the colour you typed in returned, try the above steps with the input() function again.
:#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 lab2a
</presource>
:#Before proceeding, make certain that you identify any and all errors in "lab2a.py". When the check script tells you everything is "ok", you may procede to the next step.
Sample run 1:
<presource>
run lab2b.py
Name: Jon
Age: 20
Hi Jon, you are 20 years old.
</presource>
Sample run 2:
<presource>
run lab2b.py
Name: Jen
Age: 25
Hi Jen, you are 25 years old.
</presource>
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 lab2b
</presource>
Before proceeding, make certain that you identify any and all errors in "lab2b.py". When the check script tells you everything is "ok", you may procede to the next step.
A argument is a value that is passed to a program or passed to a function. In the previous section you passed a argument to the input() function. In this section we will go over the steps of passing a argument to your script, but this time the argument will be passed when we execute your script from the bash shell.
:#In order to read arguments in Python, we will need to import special variables from the system. This is done very easily. <presource>
ipython3
import sys
</presource> This is a standard 'library' of code provided by the developers of Python. By typing 'import sys' we have loaded code written by another person, each 'library' that gets loaded will give us extra functionality in our program.
:#To inspect this library and look at all that it contains we can use the 'dir()' function. This can be used to inspect any library(and more) looking at all the functions and values contained within.<presource>
dir(sys)
</presource> Now don't feel overwhelmed, this will provide a lot of information, but we can also get some hints at the different information this library provides.
:#Now don't feel overwhelmed, this will provide a lot of information, but we can also get some hints at the different information this library provides. <presource>
print(sys.version) # tells us our python version
print(sys.platform) # tells us our operating system platform
print(sys.argv) # tells us our arguments
sys.exit() # will immediately end our script when run
</presource>
:#The 'sys.exit()' will immediately end the script on the line it is called, no other line in the script will ever be run. This function will be useful later in this lab, make sure you write it down in your notes.
Create a new script '~/ops435/lab2/showargs.py' and place the following content inside<presource>
#!/usr/bin/env python3
print('Print out the script name: ' + name)
</presource>
Run the script and examine the output: