Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 2

18 bytes added, 09:21, 22 January 2018
no edit summary
<br><br>
= INVESTIGATION 2: USING LOGIC 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 (either user input, or command arguments). In this section, we will discuss several LOGIC control flow statements including: IF, and IF/ELIF/ELSE.
if True:
print('This print is apart of the if statement')
</source>What happened? It is important to note a couple of things with the IF statement:<ul><li>When the condition 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 determined true or false.</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 as part of the main program. Also, using indentation makes it easier for a programmer to identify Control Flow statements. From this point on, be VERY careful and consistent in the indentation that you make with LOGIC conditional statements. </li></ul><br>However, if the If condition 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.
<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>
== PART 3 - Combining WHILE loops with IF statements==
:Let's improve upon your previous shell script to further prevent errors from incorrect input. You can combine LOGIC control-flow statements with other LOGIC control-flow statements for more complex programming. For example, if you ran the previous Python script without an argument (i.e. empty string), you would encounter an error since it could not convert an empty string to an integer.
:''' Perform the Following Steps '''