程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python knowledge: loop nesting

編輯:Python

One 、 explain

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 :

  1. Outer While Loop
  2. Inner While Loop

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 .

Two 、 nesting while Cycle flow chart

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 :-

  1. Since 2<=5, the outer loop gets executed

Inner loop :-

  1. Initially j=1 and since 1<=2 inners for loop is executed
  2. Print j as 1 and increment j
  3. Again check the condition, 2<=2, and the inner loop is executed
  4. Print j as 2 and increment j
  5. Now since 3<=2 is False so we get out of the inner loop

3、 ... and 、 Real time nested loop example

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 .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved