Page 126 - Computing E-Book Grade 7
P. 126
The City School 2021-2022
Sample Code
# This program compares two numbers
using elif
a = 33
b = 200
# if block
if b > a:
print(“b is greater than a”)
# elif block
elif b = = a:
print(“a and b are equal”)
In this example a is equal to b, so the first condition is not true, but the elif condition is
true, so we print to screen that “a and b are equal”.
Else Statement
The else keyword catches anything which isn’t caught by the preceding conditions. The
sample code below shows else statement.
Sample Code
# This program compares two numbers using else
statements
a = 33
b = 200
# if block
if b > a:
print(“b is greater than a”)
# elif block
elif b == a:
print(“a and b are equal”)
# else block
else
print(“a is greater than b”)
126

