Categories
Python Answers

How to add else clause on Python while statement?

Spread the love

Sometimes, we want to add else clause on Python while statement

In this article, we’ll look at how to add else clause on Python while statement.

How to add else clause on Python while statement?

To add else clause on Python while statement, we can add it below the while loop.

For instance, we write

while condition:
    handle_true()
else:
    handle_false()

to add a while loop that run while condition is True.

The else block runs when condition becomes False.

Conclusion

To add else clause on Python while statement, we can add it below the while loop.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *