Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 2

198 bytes added, 07:28, 26 May 2017
PART 1 - Using IF Statements
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 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>
:#let's create an IF statement that runs under specific conditions. Issue the following code below:<source>
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 we , you are making a comparison between the value we enter int you entered via the '''input() ''' function, which will be in turn, was saved into the password variable. The IF statement is using that variable (called password), and with the word followed by 'P@ssw0rd'. The '==' stands for is '' which represents '''equal to''', we are asking if followed by the password variable is equal to the word string ''' 'P@ssw0rd''''. Note that a spaces are used between these arguments. The IF statement tests that condition to see if it is True or False. If this that condition is True, it will run the code indented below. On the other hand, if the that condition is False, it will not run the code. Try experimenting with different combinations of passwords.
:#If statements can also be used to compare numbers, and use functions. 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. <source>
import sys
13,420
edits