Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 2

189 bytes added, 01:42, 25 May 2017
PART 1 - INDENTATION AND IF LOGIC
Indentation means to start a line with spaces or tabs before your text. In Python, indentation can control how the program itself runs. From now on be very careful and consistent in the indetation that you make. Using indentation you can choose what code runs as part of the if statement and what code runs as part of the main program.
'''Understanding If Statements ''' :'''Perform the following steps''' :#In ipython3 lets make some if statements and see what happens.<presource>
if True:
print('This print is apart of the if statement')
</presource:#When a if statement is True, it runs the code that is indented underneith it. However if the statement is False it will not run. Any code not indented under the if statement will perform normally.<presource>
if False:
print('This first print statement will never run')
print('This second print statement will also not run')
print('This print statement will run')
</presource:#These if statements are using boolean logic, this means they are either True or False. The above code uses if statement that are ALWAYS set, next lets makes a if statement that runs under specific conditions.<presource>
password = input('Please enter a secret password')
if password == 'P@ssw0rd':
print('You have succesfully used the right password')
</presource:#In the above example we are making a comparison between the value we enter int the input() function, which will be saved into the password variable, and with the word 'P@ssw0rd'. The '==' stands for is equal to, we are asking if the password variable is equal to the word 'P@ssw0rd'. If this condition is True, it will run the code indented below, if the condition is False, it will not run the code. Try experimenting with different combinations of passwords. We can also use this to compare numbers, and we will also learn a new function here as well. The function 'len()' can be used to give us the length of words and other variables. We can use this 'len()' function to give us the number of arguments provided to our script by using 'len(sys.argv)' and it should return a number. Below we are also using '!='. Which stands for not equal to.
:#We can also use this to compare numbers, and we will also learn a new function here as well. The function 'len()' can be used to give us the length of words and other variables. We can use this 'len()' function to give us the number of arguments provided to our script by using 'len(sys.argv)' and it should return a number. Below we are also using '!='. Which stands for not equal to. <presource>
import sys
sys.exit()
</presource:#This if statement means: IF the number of arguments(one) is NOT EQUAL to ten, then the condition is True. These can get a little confusing, try experimenting and move on when ready.  :#If you are running this in ipython3, the number of arguments will be always be '1'. This number will always be one higher than the actual number of arguments entered. This is because it also counts the script name as a argument. ''' lab2d '''Now it's time to create a new script. Make a copy of '''lab2c.py''' and call it '''lab2d.py'''. Now modify '''lab2d.py''', add a if statement right before your print statements. This if statement should make sure that lab2d.py is using '2' additional arguments.
The script should have a Shebang line''' Practice Using If Statements '''The script should import sysThe script should print :#Now it's time to create a usage message IF additional arguments are not givenThe new script should exit IF two additional arguments are not givenThe script should use . Make a variable called "name"The script should use copy of '''lab2c.py''' and call it '''lab2d.py'''. Now modify '''lab2d.py''', add a variable called "age"The script should use sysif statement right before your print statements.argv[1] (first argument)The script This if statement should use sysmake sure that lab2d.argv[py is using '2] (second argument)The script should store the values in the correct variablesThe script should print the EXACT OUTPUT as shown' additional arguments.
Sample run ::*The script should have a Shebang line::*The script should import sys::*The script should print a usage message IF additional arguments are not given::*The script should exit IF two additional arguments are not given::*The script should use a variable called "name"::*The script should use a variable called "age"::*The script should use sys.argv[1] (first argument)::*The script should use sys.argv[2] (second argument)::*The script should store the values in the correct variables::*The script should print the EXACT OUTPUT as shown
:::Sample run 1:<presource>
run lab2d.py Jon 20
Hi Jon, you are 20 years old.
</presource:::Sample run 2: <presource>
run lab2d.py Jen 25
Hi Jen, you are 25 years old.
</presource:::Sample run 3: <presource>
run lab2d.py
Usage: lab2d.py [name] [age]
</presource:::Sample run 4: <presource>
run lab2d.py Jon is 20
Usage: lab2d.py [name] [age]
</presource:::2. Download the check script and check your work. Enter the following commands from the bash shell.
<pre>
cd ~/ops435/lab2/
</pre>
:::3. 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 procede to the next step. 
== PART 2 - IF/ELSE/ELIF ==
198
edits