Most of us have used the if-else conditional blocks in one or the other Programming language. Python goes a step ahead and supports the use of the else conditional block in looping structures too. In this tutorial, we will see the use of the else conditional blocks within the while and else looping constructs.
Prerequisites
Please note, understanding loops and conditionals in Python is a prerequisite to this tutorial. If you are new to the Python language and want to set it up on your system, please go through this tutorial.
Let us get started
1. Using else condition within a while loop
Before we start with the else conditional block, let us do a quick recap of the while loop. In the following example, we will emulate a login page scenario where the user has a limited number of trials to enter the correct password.
### Syntax
while condition:
action
### Example
trial = 1
while trial <= 3:
pwd = input("please enter the password: ")
if pwd == "abcd":
print("Valid Password. Login Successful")
break
else:
trial += 1
### Execution
please enter the password: 123
please enter the password: abd
please enter the password: asd1
### Output
Given the user enters incorrect passwords, the code execution stops without any output
- We expected the user to enter a valid password (abcd) within three trials.
- The if-else construct validates the password, and the while keyword keeps track of the number of attempts taken by the user.
- In case of a valid password, the code prints the success message. If the password entered is incorrect, the code increases the counter by a value of one.
- On entering the correct password, the program prints the success message and terminates the loop but, if the user runs out of trials, the code ends without printing any failure message. It is where else condition can come in handy. Let us understand this with an example:
### Syntax
while condition:
action_1
else:
action_2
--------------------------------------------------------------------### Example 1 - Else Condition Executes
trial = 1
while trial <= 3:
pwd = input("please enter the password: ")
if pwd == "abcd":
print("Valid Password. Login Successful")
break
else:
trial += 1
else:
print("Invalid password entered 3 times. Login Failed")
### Execution (while loop doesn't encounter the break statement)
please enter the password: 123
please enter the password: abc
please enter the password: abc1
### Output (because condition after while keyword failed)
Invalid password entered 3 times. Login Failed
--------------------------------------------------------------------### Example 2- Else Condition Doesn't Executes
trial = 1
while trial <= 3:
pwd = input("please enter the password: ")
if pwd == "abcd":
print("Valid Password. Login Successful")
break
else:
trial += 1
else:
print("Invalid password entered 3 times. Login Failed")
### Execution (while loop encounters the break statement)
please enter the password: 123
please enter the password: abcd
### Output
Valid Password. Login Successful
- The else block present after the while loop executes only if the break statement does not terminate the loop.
- In example 1, the user has failed to enter the correct password within the permitted number of trials. In such a situation, the while loop did not encounter the break statement, and Python executed the code within the else construct.
- In example 2, the while loop encountered a break statement (after printing the success message in the 2nd trial), and the code block within else constructs does not get executed.
2. Using else with for loop
Similar to while loop, else construct can also be used with for loop. The following code block demonstrates the same:
### Syntax
for variable in iterable:
action_1
else:
action_2
### Example
for i in range(3):
pwd = input("please enter the password: ")
if pwd == "abcd":
print("Valid Password. Login Successful")
break
else:
print("Invalid password entered three times. Login Failed")
### Execution
please enter the password: 123
please enter the password: abc
please enter the password: abc1
### Output
Invalid password entered three times. Login Failed
- Similar to the examples from the while loop section, the program expects the user to enter a valid password within the allowed limit of three trials.
- Given the execution scenario, since the for loop did not encounter a break statement, the program executes the code block within the else construct and prints the failure message.
Closing note
Where else in Python language do you think we can use the else condition? Google it out and share it with the readers by commenting below.
Please do not forget to clap if this story has helped you.
HAPPY LEARNING ! ! ! !