1,760
edits
Changes
→INVESTIGATION 2: USING CONDITIONAL STATEMENTS
= INVESTIGATION 2: USING CONDITIONAL STATEMENTS =
:In computer programming, control flow statement can be used to change the direction (flow) of a program. The [https://en.wikipedia.org/wiki/Flowchart diagram here] may help you visualize it. 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 data (either user via the input() function, or command argumentsvia the list object sys.argv). In this section, we will discuss several LOGIC control flow statements including: IF, and IF/ELIF/ELSE.
== PART 1 - Using IF Statements ==
if True:
print('This print is apart of the if statement')
</source>What happened when you ran this code? It is important to note a couple of things with the IF statement:<ul><li>When the condition expression in an IF statement '''evaluates to True''', it runs the code that is indented underneath it. In this case, we can use the boolean value "True" to make this happen, or test to see if a condition expression determined true or false.</li><li>However, if the If condition expression evaluates to '''False''', then it will not run the code indented underneath it. Any code not indented under the if statement will perform normally as the main program and is NOT associated with control flow statement.</li><li>Indentation means to start a line with spaces or tabs before your text. Using '''indentation''' will direct the script what code will run as part of the IF statement and which code will run regardless. Also, using indentation makes it easier for a programmer to identify Control Flow statements. From this point on, be VERY careful and consistent with indentation because it will affect how your code works. </li></ul>
<blockquote style="margin-left:35px;">{{Admon/important|style="padding-left:25px"|4 spaces|While python allows some flexibility with your indentation - please don't be creative with it. Always use 4 spaces for each new block. There will be exceptions later on, but start with this now. You may find it helpful to configure your editor to insert for spaces instead of a tab when you press the tab key.}}</blockquote>
<ol><li value='3'>Try the following 3 lines, indenting the second and third lines, but NOT the fourth line:<source lang="python">
print('This second print statement will also not run')
print('This print statement will run')
</source>What do you notice?<br><br>So far, you have been using only the '''Boolean values True or False''' for your IF statements. Although this can be useful, it can be more practical to state a condition an expression that will be evaluated to test in order to render a '''True''' or '''False''' Boolean value to be used with control flow statements (referred to as: '''Boolean Logic''').<br><br></li>
<li>let's create an IF statement that runs under specific conditions. Issue the following code below:<source lang="python">
password = input('Please enter a secret password')
if password == 'P@ssw0rd':
print('You have succesfully used the right password')
</source>What happened? In the above example, you are making a comparison between the value you entered via the '''input()''' function, which in turn, was saved into the '''password''' variableobject. The IF statement is using that variable object (called named password), followed by '''==''' which represents '''identical to''', followed by the string ''' 'P@ssw0rd' '''. Never use '''=''' to 'compare values since this is used to store the value into a variable an object and may not allow IF statement to properly test evaluate the conditionexpression! Also note that a '''space is used to separate arguments with the IF statement'''. The IF statement tests that condition expression to see if it is '''True or False'''. If that condition expression is '''True''', it will run the code indented below. On the other hand, if that condition expression is '''False''', it will not run the code. Try experimenting with different combinations of passwords.<br><br>If statements can also be used to compare numbers. We can do this by using comparison operators (such as: '''==''', '''!=''', '''>''', '''>=''', '''<''', '''<='''), but we can also use functions. The function '''len()''' can be used to return the number of characters in a word (plus other features). length of words and other variablesobjects. We can also use the '''len()''' function to give us the number of argumuents provided to our script by using 'len(sys.argv)' and it should return a number. Below we are also using '!='. Which stands for not eqal to. <br><br></li>
<li>Try the following program:<source lang="python">
import sys
:::*The script should print a '''usage message''' if zero arguments present, or if not exactly 2 arguments are provided when running the script<br>(NOTE: Use '''sys.argv[0]''' value in this message in case you rename the script at a later date!)
:::*The script should exit without attempting to do any more work if exactly 2 arguments are not provided when running script
:::*The script should use a variable an object called '''name''':::*The script should use a variable an object 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 variablesobjects
:::*The script should print the EXACT OUTPUT as shown
:There are many ways to use IF statements in more advanced configurations. This section will provide a few more examples and provide an explanation of how they work.
#For the following examples, try changing the numbers in the variables objects to get different results.
#Create one or more new files for testing the following steps (you won't need to submit them).
#Let's use an IF statement testing a condition of two variable object values. In this case, if variable object 'a' is greater than variable object 'b', then print a message, if False, do nothing.<source lang="python">
a = 10
b = 15
else:
print('b is greater than a')
</source>What happened?<br><br>This is neat, but what if 'a' is equal to 'b'? In order to make this work, we would need to perform another test! The 'elif' statement allows us to string together multiple if statement. This new statement 'elif' means: IF the first condition expression is False, it will check the second condition expression under 'elif'. HOWEVER, if the first condition expression is True, it will run the code indented under the first condition expression and SKIP the 'elif' statement. Finally, we include the ELSE statement - in this case, if 'a' is equal to 'b' (i.e. fails first test since 'a' is not greater than 'b' and fails second test since 'a' is not less than 'b', so they must be equal to check other).<br><br>
#Modify your program to looks like this:<source lang="python">a = 10
b = 10