Skip to main content

While Loop in python : How to become a Python Jedi - Beginners Python Programming Tutorial - Part 6



Welcome back friends to another post on TheCodingProject and we are back with the sixth chapter of the Beginner Python programming tutorial - How to become a python Jedi. This time we are going to learn how to use while loop in python.

The While Loop statement?
In Python Programming, There are 3 types of loops →

  • While loop
  • For loop
  • Nested loops

Now to get into some action→

Let’s consider that two groups of aliens have landed on earth  out of these two one of the group is good & the other are the bad guys. The govt. Has given you the task of designing a system that takes in input from different eyewitnesses & then classify that whether the aliens that these eyewitnesses encounter are the good guys or bad guys.


Monetize your website traffic with yX Media



aliens = input() # ask for user input
while aliens is not "Autobots":
print(
"Who is your leader : ") # ask for user input
leader = input()
if leader == "Megatron": # if resolves to True
print(
"You are one of the bad guys") # print this statement and exit
break




Copy & paste the above code in your editor and run this code. What do you get ? When the input is Autobots it matches the condition aliens == "Autobots" & it enters the loop to prints the statement pointed by the print method.

We can also use negative conditions for entering the while loop. Copy and paste the below code in your editor and run it→






aliens = input() # ask for user input
while aliens is not "Autobots":
print(
"Who is your leader : ") # ask for user input
leader = input()
if leader == "Megatron": # if resolves to True
print(
"You are one of the bad guys") # print this statement and exit
break






As you can see the condition aliens is not "Autobots" resolves to False when the user inputs a wrong condition & the control enters the loop and print the statement when a false condition is returned.

Now let’s make our system a bit more robust by adding some extra checks into it. We shall insert an if statement inside our while loop. In the version below if user gives the first input as Decepticons then the control enters the loop & prompts user for second input. Again if the input is Megatron then the control enters the if condition to print the last statement.


aliens = input() # ask for user input
while aliens is not "Autobots":
print(
"Who is your leader : ") # ask for user input
leader = input()
if leader == "Megatron": # if resolves to True
print(
"You are one of the bad guys") # print this statement and exit
break


So, folks this was all for this week but we shall return with another chapter of this tutorial with some new concepts to learn.

Subscribe to my youtube channel to watch the coding in this tutorial in action.

If you have any questions, suggestions or comments you can post your comment here or you can also bug me on Quora, Twitter or on Facebook.

Take me to Python Tutorial - Part 5 Take me to Python Tutorial - Part 7

Popular posts from this blog

What I learned from writing a guest post

                                                     Recently I wrote a python tutorial for Datacamp , while writing this tutorial I learned a couple of things which I am going to share with you right here in my blog post. May be you can use my experiences in your own endeavours as a writer. Idea The first step is idea generation. You can either search through any previous topics in your blog post if you have one or you can go through your past learning, experiences or any problem statement that you might have gone through and you will find that while doing that you can get tons of ideas to start your article. If you fall short of ideas then you can ask for help from may be your social media followings your friends or your family, for example I asked the people at Datacamp  themselves and they suggested me a couple of topics which got me st...

Expressions in Python : How to become a Python programming Jedi - A Python programming Tutorial Part 1

Long Long ago in a Galaxy far far away there was a planet named Quora. There was a growing agitation among the planet’s inhabitants to learn the way of the Python programming Force and become a master in the way of the Python programming Jedi, but very few knew that the secret art of the Jedi is embedded in a book named as TheCodingProject…. So, let’s begin the journey of learning the secret art of the Python programming Force and we shall together make the journey towards becoming the Python Jedi…. What is Python? As per Wikipedia “Python is a widely used high-level , general-purpose , interpreted , dynamic programming language .” Python is an open source programming language which was conceived in the late 1980s and its implementation began in December 1989 by Guido van Rossum (more on it here ). For us it’s sufficient to know that Python is : High level language - Python is a high-level programming language because it provides strong “:abstractio...

My new Youtube channel

New YouTube channel I always say that thecodingproject exists because of you, it exists because I want to share with you the knowledge and experience that I have gathered over time and make it available for all of you so that you can pursue your passion and love for coding. It's been my continuous endeavor to present you content that is both interesting and useful so that learning coding is no longer boring. To achieve this I have launched my first YouTube channel . Now all of my python coding tips & tricks are also available on YouTube. Visit the link and subscribe to my channel .