Lesson 3. USING IF ELSE STATEMENTS
Lesson 3. USING IF ELSE STATEMENTS
Lesson
3
Using
If-Else
Statements
UNIT 3: Performing Conditional
Programming Using Python
If-else conditional structure
evaluates a condition and provides
statements when the answer is either true
or false.
True
Condition Statements
False
Statements
If-else statement
adds another statement when the
condition is false.
If-else in Python
adds else and its statement.
In Python, its syntax is:
if condition:
statement/s
else:
statement/s
The first half of the syntax is the same as the if
statement.
The else with a colon (:) is added to the next line.
The statement/s for the else is placed afterward.
Example 1: PIN
Let us create a program that
asks the user to enter his or her PIN.
The program displays a message if
the PIN is correct. Else, it displays
another message if the PIN is
incorrect.
Pseudocode:
1. Assign the PIN to a variable.
2. Display the instruction to the user to enter his or
her PIN.
3. Accept the entered PIN.
4. Evaluate if the entered PIN is the same as the
stored PIN.
5. Display
Displaya message
another when the PIN
message is correct.
when the PIN is
incorrect.
Start
Code = “1234”
Flowchart Output
“Please enter
the PIN:”
Enter PIN
Output “The
PIN is
incorrect.” End
Program:
#PIN
#Display a message when the PIN is the same as the
stored PIN.
#”Else, it displays another message.
code = “1234”
PIN = input(“Please enter the PIN: ”)
if PIN ==code:
print(“The PIN is correct. ”)
else:
print(“The PIN is incorrect. ”)
Output:
The program displays another message
if the user enters a different PIN.
passingscore = “7.5”
score = float(input(“Please enter your score: ”))
if score >=passingscore:
print(“You passed the quiz!”)
else:
print(“Oops. Do better next time.”)
Output:
The program displays another remark if
the score did not meet the passing mark.