Hướng dẫn how to run while loop 5 times in python - cách chạy vòng lặp while 5 lần trong python

Các vòng lặp được sử dụng trong lập trình để lặp lại một khối mã cụ thể. Trong bài viết này, bạn sẽ học cách tạo một vòng lặp trong thời gian trong Python.

Show

Video: Python trong khi vòng lặp

Vòng lặp trong Python là gì?

Vòng lặp trong khi trong Python được sử dụng để lặp lại một khối mã miễn là biểu thức kiểm tra (điều kiện) là đúng.

Chúng tôi thường sử dụng vòng lặp này khi chúng tôi không biết số lần lặp lại trước.

Cú pháp trong khi vòng lặp trong Python

while test_expression:
    Body of while

Trong vòng lặp trong khi, biểu thức kiểm tra được kiểm tra trước. Cơ thể của vòng lặp chỉ được nhập nếu

Enter n: 10
The sum is 55
9 đánh giá là
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
0. Sau một lần lặp, biểu thức kiểm tra được kiểm tra lại. Quá trình này tiếp tục cho đến khi
Enter n: 10
The sum is 55
9 đánh giá thành
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
2.

Trong Python, cơ thể của vòng lặp được xác định thông qua thụt lề.

Cơ thể bắt đầu với vết lõm và dòng chưa được khai thác đầu tiên đánh dấu sự kết thúc.

Python diễn giải bất kỳ giá trị khác không là

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
0.
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
4 và
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
5 được hiểu là
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
2.

Sơ đồ của vòng lặp trong khi

Hướng dẫn how to run while loop 5 times in python - cách chạy vòng lặp while 5 lần trong python
Sơ đồ trong khi vòng lặp trong Python

Ví dụ: Python trong khi vòng lặp

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)

Khi bạn chạy chương trình, đầu ra sẽ là:

Enter n: 10
The sum is 55

Trong chương trình trên, biểu thức kiểm tra sẽ là

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
0 miễn là biến bộ đếm I của chúng tôi nhỏ hơn hoặc bằng n (10 trong chương trình của chúng tôi).

Chúng ta cần tăng giá trị của biến bộ đếm trong phần thân của vòng lặp. Điều này rất quan trọng (và chủ yếu là bị lãng quên). Không làm như vậy sẽ dẫn đến một vòng lặp vô hạn (vòng lặp không bao giờ kết thúc).

Cuối cùng, kết quả được hiển thị.


Trong khi vòng lặp với những người khác

Tương tự như với các vòng lặp, trong khi các vòng lặp cũng có thể có khối

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 tùy chọn.

Phần

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 được thực thi nếu điều kiện trong vòng lặp trong khi đánh giá thành
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
2.

Vòng lặp trong khi có thể được chấm dứt với một tuyên bố phá vỡ. Trong những trường hợp như vậy, phần

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 bị bỏ qua. Do đó, phần
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 của một vòng lặp chạy nếu không có sự phá vỡ xảy ra và điều kiện là sai.

Dưới đây là một ví dụ để minh họa điều này.

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")

Đầu ra

Inside loop
Inside loop
Inside loop
Inside else

Ở đây, chúng tôi sử dụng một biến bộ đếm để in chuỗi bên trong vòng ba lần.

Trên lần lặp thứ tư, điều kiện trong

Inside loop
Inside loop
Inside loop
Inside else
3 trở thành
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
2. Do đó, phần
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 được thực thi.

Mục lục

  • Vòng lặp trong Python là gì?
    • Cú pháp trong khi vòng lặp trong Python
    • Trong vòng lặp trong khi, biểu thức kiểm tra được kiểm tra trước. Cơ thể của vòng lặp chỉ được nhập nếu
      Enter n: 10
      The sum is 55
      9 đánh giá là
      '''Example to illustrate
      the use of else statement
      with the while loop'''
      
      counter = 0
      
      while counter < 3:
          print("Inside loop")
          counter = counter + 1
      else:
          print("Inside else")
      0. Sau một lần lặp, biểu thức kiểm tra được kiểm tra lại. Quá trình này tiếp tục cho đến khi
      Enter n: 10
      The sum is 55
      9 đánh giá thành
      '''Example to illustrate
      the use of else statement
      with the while loop'''
      
      counter = 0
      
      while counter < 3:
          print("Inside loop")
          counter = counter + 1
      else:
          print("Inside else")
      2.
    • Ví dụ: Python trong khi vòng lặp
  • Trong khi vòng lặp với những người khác

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem nó cùng với hướng dẫn bằng văn bản để hiểu sâu hơn về sự hiểu biết của bạn: Làm chủ trong khi các vòng lặp This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Mastering While Loops

Làm thế nào để bạn chạy một vòng 4 lần trong Python? means executing the same block of code over and over, potentially many times. A programming structure that implements iteration is called a loop.

Để lặp qua một bộ mã, một số lần được chỉ định, chúng ta có thể sử dụng hàm phạm vi (), hàm phạm vi () trả về một chuỗi số, bắt đầu từ 0 theo mặc định và tăng thêm 1 (theo mặc định) và kết thúc tại một số cụ thể.

  • Làm thế nào để bạn chạy một vòng lặp 10 lần?indefinite iteration, the number of times the loop is executed isn’t specified explicitly in advance. Rather, the designated block is executed repeatedly as long as some condition is met.

  • Nếu bạn muốn vòng lặp chạy mười lần, bạn thay đổi điều kiện sao cho nó đánh giá là sai khi biểu thức tăng đã chạy mười lần. Vòng lặp chạy mười lần. Sau mười lần, điều kiện đánh giá là sai.definite iteration, the number of times the designated block will be executed is specified explicitly at the time the loop starts.

Trong hướng dẫn này, bạn sẽ:

  • Tìm hiểu về vòng lặp
    Inside loop
    Inside loop
    Inside loop
    Inside else
    3, cấu trúc điều khiển Python được sử dụng để lặp lại không xác định
  • Xem cách thoát ra khỏi vòng lặp hoặc vòng lặp lặp lại sớm
  • Khám phá vòng lặp vô hạn

Khi bạn hoàn thành, bạn nên nắm bắt tốt cách sử dụng phép lặp không xác định trong Python.

Vòng lặp Inside loop Inside loop Inside loop Inside else3

Hãy cùng xem cách mà câu lệnh Python từ

Inside loop
Inside loop
Inside loop
Inside else
3 được sử dụng để xây dựng các vòng lặp. Chúng tôi sẽ bắt đầu đơn giản và tô điểm khi chúng tôi đi.

Định dạng của vòng lặp

Inside loop
Inside loop
Inside loop
Inside else
3 thô sơ được hiển thị bên dưới:

while <expr>:
    <statement(s)>

while <expr>:
    <statement(s)>
0 đại diện cho khối được thực hiện nhiều lần, thường được gọi là thân của vòng lặp. Điều này được biểu thị bằng vết lõm, giống như trong một tuyên bố
while <expr>:
    <statement(s)>
1.

Biểu thức kiểm soát,

while <expr>:
    <statement(s)>
2, thường liên quan đến một hoặc nhiều biến được khởi tạo trước khi bắt đầu vòng lặp và sau đó được sửa đổi ở đâu đó trong thân vòng.

Khi gặp phải một vòng

Inside loop
Inside loop
Inside loop
Inside else
3,
while <expr>:
    <statement(s)>
2 lần đầu tiên được đánh giá trong bối cảnh Boolean. Nếu đó là sự thật, cơ thể vòng lặp được thực thi. Sau đó
while <expr>:
    <statement(s)>
2 được kiểm tra lại và nếu vẫn đúng, cơ thể được thực thi lại. Điều này tiếp tục cho đến khi
while <expr>:
    <statement(s)>
2 trở thành sai, tại thời điểm thực hiện chương trình tiến hành tuyên bố đầu tiên vượt ra ngoài thân vòng.

Xem xét vòng lặp này:

>>>

 1>>> n = 5
 2>>> while n > 0:
 3...     n -= 1
 4...     print(n)
 5...
 64
 73
 82
 91
100

Ở đây, những gì mà xảy ra trong ví dụ này:

  • while <expr>:
        <statement(s)>
    
    7 ban đầu là
    while <expr>:
        <statement(s)>
    
    8. Biểu thức trong tiêu đề câu lệnh
    Inside loop
    Inside loop
    Inside loop
    Inside else
    3 trên dòng 2 là
     1>>> n = 5
     2>>> while n > 0:
     3...     n -= 1
     4...     print(n)
     5...
     64
     73
     82
     91
    100
    
    0, điều này là đúng, do đó, cơ thể vòng lặp thực thi. Bên trong thân vòng trên dòng 3,
    while <expr>:
        <statement(s)>
    
    7 bị giảm bởi
     1>>> n = 5
     2>>> while n > 0:
     3...     n -= 1
     4...     print(n)
     5...
     64
     73
     82
     91
    100
    
    2 xuống
     1>>> n = 5
     2>>> while n > 0:
     3...     n -= 1
     4...     print(n)
     5...
     64
     73
     82
     91
    100
    
    3, sau đó được in.

  • Khi phần thân của vòng lặp kết thúc, việc thực thi chương trình sẽ trở lại đầu vòng lặp ở dòng 2 và biểu thức được đánh giá lại. Nó vẫn còn đúng, vì vậy cơ thể thực hiện một lần nữa và

     1>>> n = 5
     2>>> while n > 0:
     3...     n -= 1
     4...     print(n)
     5...
     64
     73
     82
     91
    100
    
    4 được in.

  • Điều này tiếp tục cho đến khi

    while <expr>:
        <statement(s)>
    
    7 trở thành
    '''Example to illustrate
    the use of else statement
    with the while loop'''
    
    counter = 0
    
    while counter < 3:
        print("Inside loop")
        counter = counter + 1
    else:
        print("Inside else")
    5. Tại thời điểm đó, khi biểu thức được kiểm tra, nó là sai và vòng lặp chấm dứt. Việc thực hiện sẽ tiếp tục tại câu lệnh đầu tiên theo cơ thể vòng lặp, nhưng có một trong trường hợp này.

Lưu ý rằng biểu thức kiểm soát của vòng

Inside loop
Inside loop
Inside loop
Inside else
3 được kiểm tra trước, trước khi bất cứ điều gì khác xảy ra. Nếu nó sai khi bắt đầu, cơ thể vòng lặp sẽ không bao giờ được thực thi:

>>>

>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...

Ở đây, những gì mà xảy ra trong ví dụ này:

while <expr>:
    <statement(s)>
7 ban đầu là
while <expr>:
    <statement(s)>
8. Biểu thức trong tiêu đề câu lệnh
Inside loop
Inside loop
Inside loop
Inside else
3 trên dòng 2 là
 1>>> n = 5
 2>>> while n > 0:
 3...     n -= 1
 4...     print(n)
 5...
 64
 73
 82
 91
100
0, điều này là đúng, do đó, cơ thể vòng lặp thực thi. Bên trong thân vòng trên dòng 3,
while <expr>:
    <statement(s)>
7 bị giảm bởi
 1>>> n = 5
 2>>> while n > 0:
 3...     n -= 1
 4...     print(n)
 5...
 64
 73
 82
 91
100
2 xuống
 1>>> n = 5
 2>>> while n > 0:
 3...     n -= 1
 4...     print(n)
 5...
 64
 73
 82
 91
100
3, sau đó được in.

>>>

>>> a = ['foo', 'bar', 'baz']
>>> while a:
...     print(a.pop(-1))
...
baz
bar
foo

Ở đây, những gì mà xảy ra trong ví dụ này:

while <expr>: <statement(s)> 7 ban đầu là while <expr>: <statement(s)> 8. Biểu thức trong tiêu đề câu lệnh Inside loop Inside loop Inside loop Inside else3 trên dòng 2 là 1>>> n = 5 2>>> while n > 0: 3... n -= 1 4... print(n) 5... 64 73 82 91 100 0, điều này là đúng, do đó, cơ thể vòng lặp thực thi. Bên trong thân vòng trên dòng 3, while <expr>: <statement(s)> 7 bị giảm bởi 1>>> n = 5 2>>> while n > 0: 3... n -= 1 4... print(n) 5... 64 73 82 91 100 2 xuống 1>>> n = 5 2>>> while n > 0: 3... n -= 1 4... print(n) 5... 64 73 82 91 100 3, sau đó được in.

Khi phần thân của vòng lặp kết thúc, việc thực thi chương trình sẽ trở lại đầu vòng lặp ở dòng 2 và biểu thức được đánh giá lại. Nó vẫn còn đúng, vì vậy cơ thể thực hiện một lần nữa và

 1>>> n = 5
 2>>> while n > 0:
 3...     n -= 1
 4...     print(n)
 5...
 64
 73
 82
 91
100
4 được in.

  • Điều này tiếp tục cho đến khi

    while <expr>:
        <statement(s)>
    
    7 trở thành
    '''Example to illustrate
    the use of else statement
    with the while loop'''
    
    counter = 0
    
    while counter < 3:
        print("Inside loop")
        counter = counter + 1
    else:
        print("Inside else")
    5. Tại thời điểm đó, khi biểu thức được kiểm tra, nó là sai và vòng lặp chấm dứt. Việc thực hiện sẽ tiếp tục tại câu lệnh đầu tiên theo cơ thể vòng lặp, nhưng có một trong trường hợp này.
    >>> n = 0
    >>> while n > 0:
    ...     n -= 1
    ...     print(n)
    ...
    
    5
    statement immediately terminates a loop entirely. Program execution proceeds to the first statement following the loop body.

  • Lưu ý rằng biểu thức kiểm soát của vòng

    Inside loop
    Inside loop
    Inside loop
    Inside else
    3 được kiểm tra trước, trước khi bất cứ điều gì khác xảy ra. Nếu nó sai khi bắt đầu, cơ thể vòng lặp sẽ không bao giờ được thực thi:
    >>> n = 0
    >>> while n > 0:
    ...     n -= 1
    ...     print(n)
    ...
    
    6
    statement immediately terminates the current loop iteration. Execution jumps to the top of the loop, and the controlling expression is re-evaluated to determine whether the loop will execute again or terminate.

Trong ví dụ trên, khi gặp vòng lặp,

while <expr>:
    <statement(s)>
7 là
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
5. Biểu thức kiểm soát
 1>>> n = 5
 2>>> while n > 0:
 3...     n -= 1
 4...     print(n)
 5...
 64
 73
 82
 91
100
0 đã sai, vì vậy cơ thể vòng lặp không bao giờ thực thi.

Hướng dẫn how to run while loop 5 times in python - cách chạy vòng lặp while 5 lần trong python
Ở đây, một vòng lặp
Inside loop
Inside loop
Inside loop
Inside else
3 khác liên quan đến một danh sách, thay vì so sánh số:

Khi một danh sách được đánh giá trong bối cảnh Boolean, đó là sự thật nếu nó có các yếu tố trong đó và giả nếu nó trống rỗng. Trong ví dụ này,

>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
2 là đúng miễn là nó có các yếu tố trong đó. Khi tất cả các mục đã được xóa bằng phương pháp
>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
3 và danh sách trống,
>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
2 là sai và vòng lặp chấm dứt.

 1n = 5
 2while n > 0:
 3    n -= 1
 4    if n == 2:
 5        break
 6    print(n)
 7print('Loop ended.')

Các câu lệnh Python

>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
5 và
>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
6

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
0

Trong mỗi ví dụ bạn đã thấy cho đến nay, toàn bộ phần thân của vòng

Inside loop
Inside loop
Inside loop
Inside else
3 được thực hiện trên mỗi lần lặp. Python cung cấp hai từ khóa chấm dứt lặp lại vòng lặp sớm:

Tuyên bố Python

>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
5 ngay lập tức chấm dứt hoàn toàn một vòng lặp. Thực hiện chương trình tiến hành tuyên bố đầu tiên sau cơ thể vòng lặp.

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
1

Tuyên bố Python

>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
6 ngay lập tức chấm dứt lặp lại vòng lặp hiện tại. Việc thực thi nhảy lên đỉnh của vòng lặp và biểu thức kiểm soát được đánh giá lại để xác định xem vòng lặp sẽ thực thi lại hay chấm dứt.

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
2

Sự khác biệt giữa

>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
5 và
>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
6 được thể hiện trong sơ đồ sau:

phá vỡ và tiếp tục

Ở đây, một tệp tập lệnh có tên là

>>> a = ['foo', 'bar', 'baz']
>>> while a:
...     print(a.pop(-1))
...
baz
bar
foo
2 thể hiện câu lệnh
>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
5:

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
3

Chạy

>>> a = ['foo', 'bar', 'baz']
>>> while a:
...     print(a.pop(-1))
...
baz
bar
foo
2 từ trình thông dịch dòng lệnh tạo ra đầu ra sau:

Hướng dẫn how to run while loop 5 times in python - cách chạy vòng lặp while 5 lần trong python

Về bây giờ, bạn có thể đang nghĩ, "Điều đó hữu ích như thế nào?" Bạn có thể hoàn thành điều tương tự bằng cách đặt những câu lệnh đó ngay sau vòng lặp

Inside loop
Inside loop
Inside loop
Inside else
3 mà không cần
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8:

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
4

Có gì khác biệt?

Trong trường hợp sau, không có điều khoản

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8,
# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
02 sẽ được thực thi sau khi vòng lặp
Inside loop
Inside loop
Inside loop
Inside else
3 chấm dứt, bất kể điều gì.

Khi

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
02 được đặt trong một điều khoản
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8, chúng sẽ chỉ được thực thi nếu vòng lặp chấm dứt bởi vì kiệt sức, thì đó là, nếu vòng lặp lặp lại cho đến khi điều kiện kiểm soát trở nên sai. Nếu vòng lặp được thoát ra bởi một tuyên bố
>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
5, mệnh đề
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 won won sẽ được thực thi.

Xem xét ví dụ sau:

>>>

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
5

Trong trường hợp này, vòng lặp lặp lại cho đến khi điều kiện cạn kiệt:

while <expr>:
    <statement(s)>
7 đã trở thành
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
5, do đó
 1>>> n = 5
 2>>> while n > 0:
 3...     n -= 1
 4...     print(n)
 5...
 64
 73
 82
 91
100
0 đã trở nên sai. Bởi vì vòng lặp sống cuộc sống tự nhiên của nó, có thể nói, điều khoản
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 đã được thực hiện. Bây giờ quan sát sự khác biệt ở đây:

>>>

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
6

Trong trường hợp này, vòng lặp lặp lại cho đến khi điều kiện cạn kiệt:

while <expr>:
    <statement(s)>
7 đã trở thành
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
5, do đó
 1>>> n = 5
 2>>> while n > 0:
 3...     n -= 1
 4...     print(n)
 5...
 64
 73
 82
 91
100
0 đã trở nên sai. Bởi vì vòng lặp sống cuộc sống tự nhiên của nó, có thể nói, điều khoản
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 đã được thực hiện. Bây giờ quan sát sự khác biệt ở đây:

Vòng lặp này được chấm dứt sớm với

>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
5, do đó mệnh đề
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 không được thực hiện.

Có vẻ như ý nghĩa của từ

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 không phù hợp với vòng lặp
Inside loop
Inside loop
Inside loop
Inside else
3 cũng như câu lệnh
while <expr>:
    <statement(s)>
1. Guido Van Rossum, người tạo ra Python, thực sự đã nói rằng, nếu anh ta phải làm lại từ đầu, anh ta đã rời khỏi điều khoản
Inside loop
Inside loop
Inside loop
Inside else
3 ____ ____38 ra khỏi ngôn ngữ.

  • Một trong những cách giải thích sau đây có thể giúp làm cho nó trực quan hơn:

  • Hãy nghĩ về tiêu đề của vòng lặp (

    # Program to add natural
    # numbers up to 
    # sum = 1+2+3+...+n
    
    # To take input from the user,
    # n = int(input("Enter n: "))
    
    n = 10
    
    # initialize sum and counter
    sum = 0
    i = 1
    
    while i <= n:
        sum = sum + i
        i = i+1    # update counter
    
    # print the sum
    print("The sum is", sum)
    25) như một câu lệnh
    while <expr>:
        <statement(s)>
    
    1 (
    # Program to add natural
    # numbers up to 
    # sum = 1+2+3+...+n
    
    # To take input from the user,
    # n = int(input("Enter n: "))
    
    n = 10
    
    # initialize sum and counter
    sum = 0
    i = 1
    
    while i <= n:
        sum = sum + i
        i = i+1    # update counter
    
    # print the sum
    print("The sum is", sum)
    27) được thực thi nhiều lần, với mệnh đề
    '''Example to illustrate
    the use of else statement
    with the while loop'''
    
    counter = 0
    
    while counter < 3:
        print("Inside loop")
        counter = counter + 1
    else:
        print("Inside else")
    8 cuối cùng đã được thực thi khi điều kiện trở nên sai.

Hãy nghĩ về

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 như thể đó là
# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
30, trong đó khối sau đó được thực thi nếu có một
>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
5.

Nếu bạn không tìm thấy một trong hai cách giải thích này hữu ích, thì hãy bỏ qua chúng.

>>>

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
7

Trong trường hợp này, vòng lặp lặp lại cho đến khi điều kiện cạn kiệt:

while <expr>:
    <statement(s)>
7 đã trở thành
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
5, do đó
 1>>> n = 5
 2>>> while n > 0:
 3...     n -= 1
 4...     print(n)
 5...
 64
 73
 82
 91
100
0 đã trở nên sai. Bởi vì vòng lặp sống cuộc sống tự nhiên của nó, có thể nói, điều khoản
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 đã được thực hiện. Bây giờ quan sát sự khác biệt ở đây:

Vòng lặp này được chấm dứt sớm với >>> n = 0 >>> while n > 0: ... n -= 1 ... print(n) ... 5, do đó mệnh đề '''Example to illustrate the use of else statement with the while loop''' counter = 0 while counter < 3: print("Inside loop") counter = counter + 1 else: print("Inside else")8 không được thực hiện.

Có vẻ như ý nghĩa của từ

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 không phù hợp với vòng lặp
Inside loop
Inside loop
Inside loop
Inside else
3 cũng như câu lệnh
while <expr>:
    <statement(s)>
1. Guido Van Rossum, người tạo ra Python, thực sự đã nói rằng, nếu anh ta phải làm lại từ đầu, anh ta đã rời khỏi điều khoản
Inside loop
Inside loop
Inside loop
Inside else
3 ____ ____38 ra khỏi ngôn ngữ.

Một trong những cách giải thích sau đây có thể giúp làm cho nó trực quan hơn:

>>>

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
8

Trong trường hợp này, vòng lặp lặp lại cho đến khi điều kiện cạn kiệt:

while <expr>:
    <statement(s)>
7 đã trở thành
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
5, do đó
 1>>> n = 5
 2>>> while n > 0:
 3...     n -= 1
 4...     print(n)
 5...
 64
 73
 82
 91
100
0 đã trở nên sai. Bởi vì vòng lặp sống cuộc sống tự nhiên của nó, có thể nói, điều khoản
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 đã được thực hiện. Bây giờ quan sát sự khác biệt ở đây:Ctrl+C, which generates an interrupt from the keyboard. Otherwise, it would have gone on unendingly. Many
# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
39 output lines have been removed and replaced by the vertical ellipsis in the output shown.

Vòng lặp này được chấm dứt sớm với

>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
5, do đó mệnh đề
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 không được thực hiện.

Có vẻ như ý nghĩa của từ

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 không phù hợp với vòng lặp
Inside loop
Inside loop
Inside loop
Inside else
3 cũng như câu lệnh
while <expr>:
    <statement(s)>
1. Guido Van Rossum, người tạo ra Python, thực sự đã nói rằng, nếu anh ta phải làm lại từ đầu, anh ta đã rời khỏi điều khoản
Inside loop
Inside loop
Inside loop
Inside else
3 ____ ____38 ra khỏi ngôn ngữ.

Một trong những cách giải thích sau đây có thể giúp làm cho nó trực quan hơn:

Hãy nghĩ về tiêu đề của vòng lặp (

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
25) như một câu lệnh
while <expr>:
    <statement(s)>
1 (
# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
27) được thực thi nhiều lần, với mệnh đề
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 cuối cùng đã được thực thi khi điều kiện trở nên sai.

>>>

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
9

Trong trường hợp này, vòng lặp lặp lại cho đến khi điều kiện cạn kiệt:

while <expr>:
    <statement(s)>
7 đã trở thành
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
5, do đó
 1>>> n = 5
 2>>> while n > 0:
 3...     n -= 1
 4...     print(n)
 5...
 64
 73
 82
 91
100
0 đã trở nên sai. Bởi vì vòng lặp sống cuộc sống tự nhiên của nó, có thể nói, điều khoản
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 đã được thực hiện. Bây giờ quan sát sự khác biệt ở đây:

Vòng lặp này được chấm dứt sớm với

>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
5, do đó mệnh đề
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 không được thực hiện.

Enter n: 10
The sum is 55
0

Có vẻ như ý nghĩa của từ

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 không phù hợp với vòng lặp
Inside loop
Inside loop
Inside loop
Inside else
3 cũng như câu lệnh
while <expr>:
    <statement(s)>
1. Guido Van Rossum, người tạo ra Python, thực sự đã nói rằng, nếu anh ta phải làm lại từ đầu, anh ta đã rời khỏi điều khoản
Inside loop
Inside loop
Inside loop
Inside else
3 ____ ____38 ra khỏi ngôn ngữ.

Một trong những cách giải thích sau đây có thể giúp làm cho nó trực quan hơn:

Hãy nghĩ về tiêu đề của vòng lặp (# Program to add natural # numbers up to # sum = 1+2+3+...+n # To take input from the user, # n = int(input("Enter n: ")) n = 10 # initialize sum and counter sum = 0 i = 1 while i <= n: sum = sum + i i = i+1 # update counter # print the sum print("The sum is", sum)25) như một câu lệnh while <expr>: <statement(s)> 1 (# Program to add natural # numbers up to # sum = 1+2+3+...+n # To take input from the user, # n = int(input("Enter n: ")) n = 10 # initialize sum and counter sum = 0 i = 1 while i <= n: sum = sum + i i = i+1 # update counter # print the sum print("The sum is", sum)27) được thực thi nhiều lần, với mệnh đề '''Example to illustrate the use of else statement with the while loop''' counter = 0 while counter < 3: print("Inside loop") counter = counter + 1 else: print("Inside else")8 cuối cùng đã được thực thi khi điều kiện trở nên sai.

Hãy nghĩ về

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 như thể đó là
# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
30, trong đó khối sau đó được thực thi nếu có một
>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
5.

Enter n: 10
The sum is 55
1

Nếu bạn không tìm thấy một trong hai cách giải thích này hữu ích, thì hãy bỏ qua chúng.

>>>

Enter n: 10
The sum is 55
2

Trong trường hợp này, vòng lặp lặp lại cho đến khi điều kiện cạn kiệt:

while <expr>:
    <statement(s)>
7 đã trở thành
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
5, do đó
 1>>> n = 5
 2>>> while n > 0:
 3...     n -= 1
 4...     print(n)
 5...
 64
 73
 82
 91
100
0 đã trở nên sai. Bởi vì vòng lặp sống cuộc sống tự nhiên của nó, có thể nói, điều khoản
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 đã được thực hiện. Bây giờ quan sát sự khác biệt ở đây:

Enter n: 10
The sum is 55
3

Vòng lặp này được chấm dứt sớm với

>>> n = 0
>>> while n > 0:
...     n -= 1
...     print(n)
...
5, do đó mệnh đề
'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 không được thực hiện.

Enter n: 10
The sum is 55
4

Enter n: 10
The sum is 55
5

Trên thực tế, tất cả các cấu trúc điều khiển Python có thể được xen kẽ với nhau đến bất kỳ mức độ nào bạn cần. Đó là như vậy. Hãy tưởng tượng sẽ bực bội đến mức nào nếu có những hạn chế bất ngờ như vòng lặp

Inside loop
Inside loop
Inside loop
Inside else
3 có thể được chứa trong một câu lệnh
while <expr>:
    <statement(s)>
1, hoặc ____ ____43 Loops chỉ có thể được lồng vào nhau nhiều nhất là sâu nhất. Bạn có một thời gian rất khó nhớ tất cả.

Dường như các giới hạn số hoặc logic tùy ý được coi là dấu hiệu của thiết kế ngôn ngữ chương trình kém. Hạnh phúc thay, bạn đã thắng được nhiều người trong Python.

Vòng lặp Inside loop Inside loop Inside loop Inside else3 một dòng

Như với một câu lệnh

while <expr>:
    <statement(s)>
1, một vòng
Inside loop
Inside loop
Inside loop
Inside else
3 có thể được chỉ định trên một dòng. Nếu có nhiều câu trong khối tạo nên thân vòng, chúng có thể được phân tách bằng dấu chấm phẩy (
# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
67):

>>>

Enter n: 10
The sum is 55
6

Điều này chỉ hoạt động với các tuyên bố đơn giản mặc dù. Bạn có thể kết hợp hai câu lệnh ghép thành một dòng. Do đó, bạn có thể chỉ định vòng lặp

Inside loop
Inside loop
Inside loop
Inside else
3 trên một dòng như trên và bạn viết một câu lệnh
while <expr>:
    <statement(s)>
1 trên một dòng:

>>>

Enter n: 10
The sum is 55
7

Điều này chỉ hoạt động với các tuyên bố đơn giản mặc dù. Bạn có thể kết hợp hai câu lệnh ghép thành một dòng. Do đó, bạn có thể chỉ định vòng lặp

Inside loop
Inside loop
Inside loop
Inside else
3 trên một dòng như trên và bạn viết một câu lệnh
while <expr>:
    <statement(s)>
1 trên một dòng:

>>>

Enter n: 10
The sum is 55
8

Điều này chỉ hoạt động với các tuyên bố đơn giản mặc dù. Bạn có thể kết hợp hai câu lệnh ghép thành một dòng. Do đó, bạn có thể chỉ định vòng lặp

Inside loop
Inside loop
Inside loop
Inside else
3 trên một dòng như trên và bạn viết một câu lệnh
while <expr>:
    <statement(s)>
1 trên một dòng:

Nhưng bạn có thể làm điều này:

Hãy nhớ rằng PEP 8 không khuyến khích nhiều tuyên bố trên một dòng. Vì vậy, có lẽ bạn không nên làm bất cứ điều gì rất thường xuyên.indefinite iteration using the Python

Inside loop
Inside loop
Inside loop
Inside else
3 loop. You’re now able to:

  • Sự kết luận
  • Trong hướng dẫn này, bạn đã tìm hiểu về việc lặp lại không xác định bằng cách sử dụng vòng lặp Python
    Inside loop
    Inside loop
    Inside loop
    Inside else
    3. Bây giờ bạn có thể:
  • Xây dựng các vòng
    Inside loop
    Inside loop
    Inside loop
    Inside else
    3 cơ bản và phức tạp
  • Thực thi vòng lặp với
    >>> n = 0
    >>> while n > 0:
    ...     n -= 1
    ...     print(n)
    ...
    
    5 và
    >>> n = 0
    >>> while n > 0:
    ...     n -= 1
    ...     print(n)
    ...
    
    6

Sử dụng mệnh đề

'''Example to illustrate
the use of else statement
with the while loop'''

counter = 0

while counter < 3:
    print("Inside loop")
    counter = counter + 1
else:
    print("Inside else")
8 với vòng lặp
Inside loop
Inside loop
Inside loop
Inside else
3

Đối phó với các vòng lặp vô hạndefinite iteration with

# Program to add natural
# numbers up to 
# sum = 1+2+3+...+n

# To take input from the user,
# n = int(input("Enter n: "))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
    sum = sum + i
    i = i+1    # update counter

# print the sum
print("The sum is", sum)
76 loops—recurrent execution where the number of repetitions is specified explicitly.

Bây giờ bạn nên nắm bắt tốt cách thực hiện một đoạn mã lặp đi lặp lại. This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Mastering While Loops

Làm thế nào để bạn chạy một vòng 4 lần trong Python?

Để lặp qua một bộ mã, một số lần được chỉ định, chúng ta có thể sử dụng hàm phạm vi (), hàm phạm vi () trả về một chuỗi số, bắt đầu từ 0 theo mặc định và tăng thêm 1 (theo mặc định) và kết thúctại một số cụ thể.use the range() function, The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

Làm thế nào để bạn chạy một vòng lặp 10 lần?

Nếu bạn muốn vòng lặp chạy mười lần, bạn thay đổi điều kiện sao cho nó đánh giá là sai khi biểu thức tăng đã chạy mười lần.Vòng lặp chạy mười lần.Sau mười lần, điều kiện đánh giá là sai.change the condition such that it evaluates to false when the increment expression has ran ten times. The loop runs ten times. After ten times, the condition evaluates to false.

Làm thế nào để bạn lặp lại mã python 5 lần?

Cách lặp lại một chuỗi nhiều lần trong Python..
Xây dựng vấn đề và tổng quan về giải pháp ..
Phương pháp 1: Sử dụng toán tử print () và phép nhân ..
Phương pháp 2: Sử dụng một vòng lặp và phạm vi ().
Phương pháp 3: Sử dụng hàm đầu vào () ..
Phương pháp 4: Sử dụng itertools.repeat ().
Phương pháp 5: Sử dụng DataFrame ..
Summary..
Regex hài hước ..