Changes

Jump to: navigation, search

OPS435 Python3 Lab 2

59 bytes added, 17:00, 12 September 2019
PART 2 - Using IF/ELIF/ELSE Statements
#For the following examples, try changing the numbers in the objects to get different results.
#Create one or more new files for testing the following steps (you wondon't need to submit them).#Let's use an expression in an IF statement testing to test a condition of two object values. In this case, if we have two integer objects 'a' and 'b'. If object 'a' is greater than object 'b', then print a message, if False, do nothing.<source lang="python">
a = 10
b = 15
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 expression is False, it will check the second expression under 'elif'. HOWEVER, if the first expression is True, it will run the code indented under the first 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
if a > b:
1,760
edits

Navigation menu