codescracker


python

Python while Loop



« Previous Tutorial Next Tutorial »


Python while loop generally used to perform the same action as for loop does, but in different way. Here is the general form to use while loop in python:

while expression:
   statement(s)

Python while Loop Example

Here is an example demonstrates while loop in python:

# Python while Loop - Example Program

i=0
while(i<10):
    print(i)
    i=i+1

Here is the sample output of the above python program:

python while loop

Let's take another example program also demonstrates the use of while loop in python. This program perform the same action or the output of this program is same as the output of the program (of previous tutorial of for loop). The difference is, that program build up with for loop whereas this one with while loop:

# Python while Loop - Example Program
		
while True:
	print("Enter 'x' for exit.")
	n = input("Enter any number: ")
	if n == 'x':
		break
	else:
		val = int(n)
		nr = int(input("Upto how many rows: "))
		i = 0
		j = 0
		while i<nr:
			while j<i+1:
				print(val,end=" ")
				j = j + 1
			j = 0
			i = i + 1
			print()

Here are sample runs of the above python program of while loop. This is the first snapshot:

while loop in python

This is the second screenshot:

python while loop example

Let's take an example on while loop. This example ask a question and continue asking until the user give the right or correct answer. Let's see how this example works.

# Python while Loop Example
print("Welcome to while loop of Python.");
print("What are you doing here ?");
what = input();
while(what != "learning python"):
      what = input("What ?\n");
print("Right..!!");

Here are some sample runs of the above while loop example in python.

This is first output:

while loop python

Now type your answer and press enter key to see the output. This is the second output after entering wrong answer:

while loop example python

This is the third output again after entering the wrong answer:

while loop python example

This is the fourth output, now this time after entering the correct answer:

python while loop code

Here is another example of while loop on compound conditions with password protection feature in Python.

print("\tPassword protected system of CodesCracker");
print("\t\tMembers are allowed only\n");
username = "";
while not username:
    username = str(input("Enter username:"));
pswrd = "";
while not pswrd:
    pswrd = str(input("Enter password:"));
if username == "pythonProgrammer" and pswrd == "private":
    print("Welcome pythonProgrammer");
elif username == "pythonCoder" and pswrd == "alpha408":
    print("Welcome pythonCoder");
elif username == "deepak" and pswrd == "lovecoding":
    print("Welcome Deepak");
elif username == "devraj" and pswrd == "lovetouring":
    print("Welcome Devraj");
elif username == "dev" and pswrd == "lovesoftwaredeveloping":
    print("Welcome Dev");
elif username == "guest" and pswrd == "guest":
    print("Welcome guest");
else:
    print("Login failed!");

In this example, only members are allowed with following username and password listed in the following table:

Username Password
pythonProgrammer private
pythonCoder alpha408
deepak lovecoding
devraj lovetouring
dev lovesoftwaredeveloping
guest guest

If you will enter the username and password out of the above list, then Login failed! is the message you will see on the output screen. And if provide any of the above detail to login, then you will see the welcome message as shown in the following sample outputs.

while loop python code

If you press enter without typing any username, then the program again ask to enter the username and continue asking until you enter something for username as shown in the below screenshot:

while loop on multiple condition

Here is the output when you type username (of any member), devraj here, and wrong password, then you will see the following output:

python while loop program

Now, let's provide correct username and password to check out the output as shown in the following screenshot:

python while example

More Examples

Here are list of some more examples, uses while loop in python:


« Previous Tutorial Next Tutorial »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test