Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 2

180 bytes added, 08:37, 26 May 2017
INVESTIGATION 2: USING LOGIC STATEMENTS
= INVESTIGATION 2: USING LOGIC STATEMENTS =
:In computer programming, control flow statement can be used to change the direction (flow) of a program. 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, IF/ELSE, and IF/ELIF/ELSE.
== PART 1 - Using IF Statements ==
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''' variable. The IF statement is using that variable (called password), followed by '''==''' which represents '''equal to''', followed by the string ''' 'P@ssw0rd' '''. Note that a '''space is used to separate arguments with the IF statement'''. The IF statement tests that condition to see if it is '''True or False'''. If that condition is '''True''', it will run the code indented below. On the other hand, if that condition 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 (like ==, and !=, >, >=, <, <=), but we can also use functions. The function '''len()''' can be used to give us return the number of characters in a word (plus other features). length of words and other variables. We can also use this the '''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. :#Issue the following lines of code:<source>
import sys
13,420
edits