In a program we can make decisions based on asking questions or testing certain conditions, then we can choose the correct output based on if the condition is true or false.
Single Statement - If the question is true we perform an action, if not true the program does nothing.
if condition is true:
perform this action
If/Else Statement - If the first condition is NOT true the program runs the second part of the code after else at each instance.
if condition is true:
perform action 1
else:
perform action 2
Else If (elif) Statement(s) - we can test against multiple conditions, if neither of the first two conditions are true, the program will perform the action after else. It's important to remember that logical operation is that if condition 1 is true, then the code beneath will NOT be executed.
if condition_1 is true:
perform this action 1
elif condition_2 is true:
perform this action 2
elif condition_3 is true:
perform action 3
ELSE:
perform action 4
#simple example 1 - note the use of "double equals" to test equality
answer = 12
if answer == 12:
print("Correct!")
#use of >, >=, <, <=, !=
score = 5
if score >= 5:
print("You have passed.")
correctAnswer = 4
userAnswer = 6
if userAnswer != correctAnswer:
print("You are wrong")
#another example - again, note the use of "double equals" to test equality
answer = num1 + num2
userAnswer = int(input("Please enter the sum of num1 and num2: "))
if userAnswer == answer:
print("You're correct, well done")
#example 1
answer = 5
if answer == 5:
print("Correct")
else:
print("Incorrect")
#example 2
#this example could be VERY, VERY useful when completing your project this trimester!
answer = num1 + num2
userAnswer = int(input("Please enter the sum of num1 and num2: "))
if userAnswer == answer:
print("You're correct, well done")
score +=1
else:
print("Bzzzt, wrong!")
score -=1
#example 1
answer = int(input("Enter a number: "))
if answer > 10:
print("Your guess is too large!")
elif answer < 10:
print("Your guess is too small!")
else:
print("You entered the number 10. You guessed correct.")
#example 2 - note the use of quotes is needed around strings but not numbers
#this example could be VERY, VERY useful when completing your project this trimester!
difficulty = input("Please enter one of the following to choose difficulty - [E]asy, [M]edium, or [D]ifficult"):
if difficulty == "E":
print("You've chosen Easy.")
maximum = 10
elif difficulty == "M":
print("You've chosen Medium.")
maximum = 50
else:
print("You've chosen Difficult.")
maximum = 100
#example 3
age = int(input("Enter your age: "))
if age > 18:
print("You are old enough to vote.")
elif age == 18:
print("You are just old enough to vote.")
elif age == 17:
print("You are nearly old enough to vote.")
elif age == 16:
print("You will be allowed to vote in two years.")
else:
print("You won't be allowed to vote for a while.")
#example 4
accessLevel = 0
userName = input("Enter your userName: "))
if userName == "Mr S":
print("You have gained top level clearance into this system.")
accessLevel = 3
elif userName == "Captain":
print("You have gained mid-level clearance into this system.")
accessLevel = 2
elif userName == "Cadet":
print("You have gained low-level clearance into this system.")
accessLevel = 1
else:
print("You have gained bottom-level clearance into this system.")
accessLevel = 0
Selection - IF , ELIF and ELSE statements video
Use either if or elif before any of these sorts of comparisons to determine the path a program follows.
(==) EQUAL - If the value of the two operands are equal (the same) the condition returns true
a == b
userAnswer == answer
(!=) NOT EQUAL - If the two values are not equal the condition returns true
a != b
userGuess != 5
(>) GREATER THEN - If the value on the left is greater then the value on the right the condition will return true
a > b
b > a
(<) LESS THEN - if the value on the left is smaller then the value on the right the condition will return true
a <= b
b <= a
questions <= 10
(>=) GREATER THEN OR EQUAL - If the value on the left is greater then or equal to the value on the right the condition will return true
a = 10
b = 20
a >= b >>> false
b >= a >>> true
a >= 15 >>> false
(<=) LESS THEN OR EQUAL- if the value on the left is smaller or equal to the value on the right the condition will return true
a = 10
b = 20
c = 10
a <= b >>> false
a <= c >>> true
Further examples of using IF/ELIF/ELSE statements