The cycle is python It's very important , Because without them , We will have to repeat the instructions over and over again , This can be time-consuming for programmers . while The loop evaluates only the given conditions , If it is true , Execute a set of statements , Until the condition is true . But if the complexity of the problem increases , Then it is necessary to be in another while Insert a... Into the loop while loop . therefore , In this tutorial , You will learn Python Nesting in while loop .
Python nesting While Circular grammar ; nesting while The basic syntax of a loop is as follows :
#outer while loop while condition: #inner while loop while condition: block of code block of code
Nested while The loop consists of two basic components :
external while A loop can contain multiple internals while loop . The condition here produces a Boolean value , If it's true , Then only the loop will be executed . For each iteration of the outer loop , The inner loop is executed from the beginning , This process continues until the external circulation condition is true . Similarly , Only if its condition is true and the code block is executed , Inside while The loop will be executed .
First , Evaluate external circulation conditions . If it is false , Then the control jumps to the outside while At the end of the loop . If the condition is true , Then the control jumps to the internal while The loop condition . Next , Evaluate internal while The loop condition . If it is false , Then the control jumps back to the outside while The loop condition . If it is true , Then execute internal while Code blocks within a loop .
nesting while A simple example of a loop
In this example , We use nested while Loop to create numeric mode
i=1
while i<=5:
j=1
while j<=i:
print(j,end=" ")
j=j+1
print("")
i=i+1
Output:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
In the code above , external while Loop through each new row in the pattern , Inside while Cycle to display numbers according to conditions .
Example , if i=2
Outer loop :-
Inner loop :-
Problem statement
Consider an online Q & a game , The user must write synonyms for a given word , And only 3 A chance to try the problem correctly .
synonyms=['pretty','alluring','lovely']
counter=3
while counter>0:
answer=input("What is the synonym of 'Beautiful': ")
status=True
i=0
while i<len(synonyms):
if(synonyms[i]==answer):
print("Correct!!")
counter=-1
break
else:
status=False
i=i+1
if(status==False):
print("Incorrect!!")
counter=counter-1
print(f"You have {counter} chances")
Output:
What is the synonym of 'Beautiful': ugly Incorrect!! You have 2 chances What is the synonym of 'Beautiful': bad Incorrect!! You have 1 chances What is the synonym of 'Beautiful': pretty Correct!!
In the code above , We use a counter variable to store the number of attempts , And external while Circular validation only gives the user 3 Attempts to . We also have a list called synonyms , Used to store valid answers to questions . Inside while Loop through the list and check that the user has provided the correct answer . If the answer provided is correct , Then it just interrupts from the internal loop and exits the program , Because the counter variable is equal to -1. If the answer is not correct , Then the counter variable is decremented and the internal while loop , Until the counter variable becomes zero .