Hướng dẫn why break statement is not working in python? - tại sao câu lệnh break không hoạt động trong python?

Tuyên bố Break đề cập đến cấp độ vòng bên trong nhất

Mã dưới đây là một vòng lặp vô hạn:

while True:
    for i in range(10):
        if i == 5:
            break  # breaks the for, start a new iteration of the while loop

Để phá vỡ vòng lặp trong khi bạn có thể cân nhắc sử dụng một số loại cờ như thế này

while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break

Tuyên bố For-Else cũng có thể hữu ích ở đây:

while True:
    for ...:
         if ...:
             # break the for loop
             break  # refers to the for statement
    else:
        # the breaking condition was never met during the for loop
        continue # refers to the while statement

    # this part only execute if the for loop was broken
    break # refers to the while statement

Tóm tắt: Trong hướng dẫn này, bạn sẽ tìm hiểu về câu lệnh Python

while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
2 và cách sử dụng nó để thoát khỏi vòng lặp sớm.: in this tutorial, you’ll learn about the Python
while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
2 statement and how to use it to exit a loop prematurely.

Giới thiệu về Tuyên bố Break Python

Đôi khi, bạn muốn chấm dứt vòng lặp

while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
3 hoặc vòng lặp
while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
4 sớm bất kể kết quả của các thử nghiệm có điều kiện. Trong những trường hợp này, bạn có thể sử dụng câu lệnh
while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
2:

break

Code language: Python (python)

Thông thường, bạn sử dụng câu lệnh

while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
2 với câu lệnh
while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
7 để chấm dứt một vòng lặp khi một điều kiện là
while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
8.

Sử dụng Python Break với For Loop

Những điều sau đây cho thấy cách sử dụng câu lệnh

while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
2 bên trong vòng lặp
while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
3:

for index in range(n): # more code here if condition: break

Code language: Python (python)

Trong cú pháp này, nếu

while True:
    for ...:
         if ...:
             # break the for loop
             break  # refers to the for statement
    else:
        # the breaking condition was never met during the for loop
        continue # refers to the while statement

    # this part only execute if the for loop was broken
    break # refers to the while statement
1 đánh giá thành
while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
8, câu lệnh
while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
2 sẽ chấm dứt vòng lặp ngay lập tức. Nó đã thắng được thực hiện các lần lặp lại còn lại.

Ví dụ này cho thấy cách sử dụng câu lệnh

while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
2 bên trong vòng lặp
while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
3:

for index in range(0, 10): print(index) if index == 3: break

Code language: Python (python)

Output:

0 1 2 3

Code language: Python (python)

Làm thế nào nó hoạt động.

  • Vòng lặp
    while True:
        broken = False
        for i in xrange(10):
             if i == 5:
                 broken = True
                 # break the for loop
                 break
        if broken:
            # break the while loop
            break
    
    3 lặp lại hơn 10 số từ 0 đến 9 và hiển thị mỗi số trên màn hình.
  • Tuy nhiên, khi bộ đếm vòng là 3, câu lệnh
    while True:
        broken = False
        for i in xrange(10):
             if i == 5:
                 broken = True
                 # break the for loop
                 break
        if broken:
            # break the while loop
            break
    
    2 sẽ chấm dứt vòng lặp ngay lập tức. Do đó, chương trình chỉ hiển thị 4 số, từ 0 đến 3 trên màn hình.

Khi bạn sử dụng câu lệnh

while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
2 trong một vòng lặp lồng nhau, nó sẽ chấm dứt vòng lặp trong cùng. Ví dụ:

for x in range(5): for y in range(5): # terminate the innermost loop if y > 1: break # show coordinates on the screen print(f"({x},{y})")

Code language: Python (python)

Output:

(0,0) (0,1) (1,0) (1,1) (2,0) (2,1) (3,0) (3,1) (4,0) (4,1)

Code language: Python (python)

Ví dụ này sử dụng hai vòng

while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
3 để hiển thị tọa độ từ

break

Code language: Python (python)
0 đến

break

Code language: Python (python)
1 trên màn hình.

Tuyên bố

while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
2 trong vòng lặp lồng nhau chấm dứt vòng lặp trong cùng khi

break

Code language: Python (python)
3 lớn hơn một.

Do đó, bạn chỉ thấy các tọa độ có giá trị y bằng không và một.

Sử dụng câu lệnh Python Break với một vòng lặp lại

Sau đây cho thấy cách sử dụng câu lệnh

while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
2 bên trong vòng lặp
while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
4:

while condition: # more code if condition: break

Code language: Python (python)

Ví dụ sau đây sử dụng câu lệnh

while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
2 bên trong vòng lặp
while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
4.

Nó sẽ nhắc bạn vào màu sắc yêu thích của bạn. Chương trình sẽ dừng lại khi bạn nhập

break

Code language: Python (python)
8:

while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
0

Output:

while True:
    broken = False
    for i in xrange(10):
         if i == 5:
             broken = True
             # break the for loop
             break
    if broken:
        # break the while loop
        break
1

Làm thế nào nó hoạt động.

  • Vòng lặp
    while True:
        broken = False
        for i in xrange(10):
             if i == 5:
                 broken = True
                 # break the for loop
                 break
        if broken:
            # break the while loop
            break
    
    3 lặp lại hơn 10 số từ 0 đến 9 và hiển thị mỗi số trên màn hình.
  • Tuy nhiên, khi bộ đếm vòng là 3, câu lệnh
    while True:
        broken = False
        for i in xrange(10):
             if i == 5:
                 broken = True
                 # break the for loop
                 break
        if broken:
            # break the while loop
            break
    
    2 sẽ chấm dứt vòng lặp ngay lập tức. Do đó, chương trình chỉ hiển thị 4 số, từ 0 đến 3 trên màn hình.
  • Khi bạn sử dụng câu lệnh
    while True:
        broken = False
        for i in xrange(10):
             if i == 5:
                 broken = True
                 # break the for loop
                 break
        if broken:
            # break the while loop
            break
    
    2 trong một vòng lặp lồng nhau, nó sẽ chấm dứt vòng lặp trong cùng. Ví dụ:

Ví dụ này sử dụng hai vòng while True: broken = False for i in xrange(10): if i == 5: broken = True # break the for loop break if broken: # break the while loop break 3 để hiển thị tọa độ từ breakCode language: Python (python)0 đến breakCode language: Python (python)1 trên màn hình.

  • Tuyên bố
    while True:
        broken = False
        for i in xrange(10):
             if i == 5:
                 broken = True
                 # break the for loop
                 break
        if broken:
            # break the while loop
            break
    
    2 trong vòng lặp lồng nhau chấm dứt vòng lặp trong cùng khi

    break

    Code language: Python (python)
    3 lớn hơn một.

Do đó, bạn chỉ thấy các tọa độ có giá trị y bằng không và một.

Tại sao phá vỡ không hoạt động Python?

Tuyên bố phá vỡ không hoạt động và vòng lặp đang chạy vô hạn.Câu trả lời cho câu hỏi của bạn là câu lệnh "Break" đề cập đến vòng lặp cho vòng lặp trong khi.the "break" statement refers to the for loop and not to the while loop.

Tuyên bố phá vỡ có hoạt động trong Python không?

'Break' trong Python là một câu lệnh điều khiển vòng lặp.Nó được sử dụng để kiểm soát chuỗi của vòng lặp.Giả sử bạn muốn chấm dứt một vòng lặp và bỏ qua mã tiếp theo sau vòng lặp;Phá vỡ sẽ giúp bạn làm điều đó.. It is used to control the sequence of the loop. Suppose you want to terminate a loop and skip to the next code after the loop; break will help you do that.

Tại sao vòng lặp trong khi tôi không bị vỡ?

Nếu vòng lặp trong khi không có điều kiện cho phép nó bị hỏng, thì hình thức này được gọi là vòng lặp vô hạn.Một vòng lặp vô hạn là một vòng lặp diễn ra mãi mãi mà không có kết thúc.. An infinite loop is a loop that goes on forever with no end.

Có phá vỡ trong Python dừng tất cả các vòng không?

Tuyên bố phá vỡ Python ngay lập tức chấm dứt hoàn toàn một vòng lặp..