Python programming - while loop nested explanation (with case)

Author: Insist--

Personal homepage: insist--personal homepage

This article column: Python column

Column introduction: This column is a free column, and will continue to update the basic knowledge of python . Welcome to subscribe and pay attention.

 

Table of contents

1. While nested syntax

2. Matters needing attention

3. While nested loop case (99 multiplication table)

1. Code

 2. Analyze the code

3. Possible problems

1. What does end='' in the code do?

2. Why is print() added at the end to make it wrap?

3. Why is the condition of the outer while set a <= 9?


foreword

This article will explain the syntax of while nesting, precautions for using while and a case.

1. While nested syntax

The syntax of while loop nesting is basically the same as that of "if judgment statement", and while nesting is also indented based on spaces. while will first judge the outer loop condition, and then enter the inner loop. When the inner loop ends, return to the outer loop (upper layer) until the loop condition is not satisfied.

while 判断条件:                             #外层
    执行的语句
    while 判断条件:                         #内层
        执行语句
   #如果需要更多层的判断,以此类推

As shown above, it is the syntax of while nesting. There are 4 spaces before the statement executed by the outer while loop as indentation, while there are 8 spaces before the statement executed by the inner while fantasy as indentation. In the process of writing code, the indentation relationship must not be written wrong.

2. Matters needing attention

1. Be sure to pay attention to space indentation , which is very important.

2. Be careful not to repeat variables . For example, if the outer variable is A, then do not use variable A to define the inner variable.

3. Pay attention to the setting of conditions and try to avoid infinite loops .

3. While nested loop case (99 multiplication table)

In the process of learning Python, continuous accumulation and practice are required. Only in this way can we go further . Let's talk about how to use while to output the 99 multiplication table.

1. Code

a = 1
while a <= 9:
    b = 1
    while b <= a:
        print(f"{a}*{b}={a*b}\t",end='')
        b += 1
    a += 1
    print()

The output is as follows:

 2. Analyze the code

① First of all, we must know that our practical while nested loop knowledge has realized the 99 multiplication table.

②We also define a variable (a=1, b=1) in the while outer loop and inner loop respectively, and then make the value of a not greater than 9, and the value of b not greater than a.

③ Use the print statement to print out the content of the program running.

④ Then after each run, add 1 to the variables a and b (b+=1, a+=1).

⑤Finally use python() to wrap

Note: Pay attention to space indentation, space indentation directly determines whether your line of code belongs to the outer or inner loop.

3. Possible problems

1. What does end='' in the code do?

Answer: The picture below shows the effect of not adding end=''. You can find that the print will automatically wrap after printing, so we can use end='' to prevent it from automatically wrapping.

2. Why is print() added at the end to make it wrap?

Answer: As shown in the figure below, let’s take a look first, without the effect of print(). You can see that they are all output to one line, which is why print() is added.

3. Why is the condition of the outer while set a <= 9?

Answer: Because the 99 multiplication table has nine lines, we set a condition (while a<=9)

Guess you like

Origin blog.csdn.net/m0_73995538/article/details/131980015