Hướng dẫn how to end a while true loop in python - cách kết thúc vòng lặp while true trong python

Tôi đã viết một while loop trong một chức năng, nhưng không biết làm thế nào để ngăn chặn nó. Khi nó không đáp ứng điều kiện cuối cùng của nó, vòng lặp sẽ đi mãi mãi. Làm thế nào tôi có thể ngăn chặn nó?

def determine_period(universe_array):
    period=0
    tmp=universe_array
    while True:
        tmp=apply_rules(tmp)#aplly_rules is a another function
        period+=1
        if numpy.array_equal(tmp,universe_array) is True:
            break    #i want the loop to stop and return 0 if the 
                     #period is bigger than 12
        if period>12:  #i wrote this line to stop it..but seems it 
                       #doesnt work....help..
            return 0
        else:   
            return period

Hướng dẫn how to end a while true loop in python - cách kết thúc vòng lặp while true trong python

Termininja

6.34212 Huy hiệu vàng46 Huy hiệu bạc49 Huy hiệu đồng12 gold badges46 silver badges49 bronze badges

Hỏi ngày 15 tháng 12 năm 2008 lúc 14:35Dec 15, 2008 at 14:35

Không có tính năng nàyNONEenglisher

1.5874 Huy hiệu vàng14 Huy hiệu bạc17 Huy hiệu đồng4 gold badges14 silver badges17 bronze badges

2

Chỉ cần thụt mã chính xác mã của bạn:

def determine_period(universe_array):
    period=0
    tmp=universe_array
    while True:
        tmp=apply_rules(tmp)#aplly_rules is a another function
        period+=1
        if numpy.array_equal(tmp,universe_array) is True:
            return period
        if period>12:  #i wrote this line to stop it..but seems its doesnt work....help..
            return 0
        else:   
            return period

Bạn cần hiểu rằng câu lệnh break trong ví dụ của bạn sẽ thoát khỏi vòng lặp vô hạn mà bạn đã tạo bằng

def determine_period(universe_array):
    period=0
    tmp=universe_array
    while True:
        tmp=apply_rules(tmp)#aplly_rules is a another function
        period+=1
        if numpy.array_equal(tmp,universe_array) is True:
            return period
        if period>12:  #i wrote this line to stop it..but seems its doesnt work....help..
            return 0
        else:   
            return period
0. Vì vậy, khi điều kiện phá vỡ là đúng, chương trình sẽ thoát khỏi vòng lặp vô hạn và tiếp tục đến khối thụt lề tiếp theo. Vì không có khối sau trong mã của bạn, chức năng kết thúc và không trả lại bất cứ điều gì. Vì vậy, tôi đã sửa mã của bạn bằng cách thay thế câu lệnh break bằng câu lệnh
def determine_period(universe_array):
    period=0
    tmp=universe_array
    while True:
        tmp=apply_rules(tmp)#aplly_rules is a another function
        period+=1
        if numpy.array_equal(tmp,universe_array) is True:
            return period
        if period>12:  #i wrote this line to stop it..but seems its doesnt work....help..
            return 0
        else:   
            return period
2.

Theo ý tưởng của bạn để sử dụng một vòng lặp vô hạn, đây là cách tốt nhất để viết nó:

def determine_period(universe_array):
    period=0
    tmp=universe_array
    while True:
        tmp=apply_rules(tmp)#aplly_rules is a another function
        period+=1
        if numpy.array_equal(tmp,universe_array) is True:
            break
        if period>12:  #i wrote this line to stop it..but seems its doesnt work....help..
            period = 0
            break

    return period

Đã trả lời ngày 15 tháng 12 năm 2008 lúc 14:37Dec 15, 2008 at 14:37

Hướng dẫn how to end a while true loop in python - cách kết thúc vòng lặp while true trong python

3

def determine_period(universe_array):
    period=0
    tmp=universe_array
    while period<12:
        tmp=apply_rules(tmp)#aplly_rules is a another function
        if numpy.array_equal(tmp,universe_array) is True:
            break 
        period+=1

    return period

Đã trả lời ngày 15 tháng 12 năm 2008 lúc 14:38Dec 15, 2008 at 14:38

Joel Coehoornjoel CoehoornJoel Coehoorn

387K110 Huy hiệu vàng560 Huy hiệu bạc787 Huy hiệu đồng110 gold badges560 silver badges787 bronze badges

5

Nhà điều hành

def determine_period(universe_array):
    period=0
    tmp=universe_array
    while True:
        tmp=apply_rules(tmp)#aplly_rules is a another function
        period+=1
        if numpy.array_equal(tmp,universe_array) is True:
            return period
        if period>12:  #i wrote this line to stop it..but seems its doesnt work....help..
            return 0
        else:   
            return period
3 trong Python có lẽ không làm những gì bạn mong đợi. Thay vì điều này:

    if numpy.array_equal(tmp,universe_array) is True:
        break

Tôi sẽ viết nó như thế này:

    if numpy.array_equal(tmp,universe_array):
        break

Toán tử

def determine_period(universe_array):
    period=0
    tmp=universe_array
    while True:
        tmp=apply_rules(tmp)#aplly_rules is a another function
        period+=1
        if numpy.array_equal(tmp,universe_array) is True:
            return period
        if period>12:  #i wrote this line to stop it..but seems its doesnt work....help..
            return 0
        else:   
            return period
3 kiểm tra nhận dạng đối tượng, một cái gì đó hoàn toàn khác với bình đẳng.

Đã trả lời ngày 15 tháng 12 năm 2008 lúc 18:03Dec 15, 2008 at 18:03

Greg Hewgillgreg HewgillGreg Hewgill

914K178 Huy hiệu vàng1133 Huy hiệu bạc1267 Huy hiệu đồng178 gold badges1133 silver badges1267 bronze badges

Tôi sẽ làm điều đó bằng cách sử dụng một vòng lặp như hình dưới đây:

def determine_period(universe_array):
    tmp = universe_array
    for period in xrange(1, 13):
        tmp = apply_rules(tmp)
        if numpy.array_equal(tmp, universe_array):
            return period
    return 0

Đã trả lời ngày 16 tháng 12 năm 2008 lúc 19:03Dec 16, 2008 at 19:03

SurajsurajSuraj

4.7091 Huy hiệu vàng17 Huy hiệu bạc12 Huy hiệu đồng1 gold badge17 silver badges12 bronze badges

Đây là một đoạn mã ví dụ từ "Python hoặc mọi người" của Charles Severance về việc viết trong khi các vòng lặp thực sự:

while True:
    line = input('> ')
    if line == 'done':
        break
    print(line)
print('Done!')

Điều này đã giúp tôi với vấn đề của tôi!

Đã trả lời ngày 27 tháng 6 năm 2021 lúc 17:35Jun 27, 2021 at 17:35

1

Làm thế nào để bạn kết thúc một thời gian trong vòng lặp thực sự?

Bạn có thể dừng một vòng lặp vô hạn với ctrl + c.Bạn có thể tạo ra một vòng lặp vô hạn có chủ ý trong khi đúng.Tuyên bố phá vỡ có thể được sử dụng để dừng vòng lặp một thời gian ngay lập tức.CTRL + C . You can generate an infinite loop intentionally with while True . The break statement can be used to stop a while loop immediately.

Làm thế nào để bạn chấm dứt một thời gian đúng trong Python?

Thông thường, trong Python, một vòng lặp vô hạn được tạo ra trong khi đúng: thay vì đúng, bạn cũng có thể sử dụng bất kỳ biểu thức nào khác luôn trả về đúng.Một cách khác để chấm dứt một vòng lặp vô hạn là nhấn ctrl+c.Khi viết các vòng lặp vô hạn, hãy chắc chắn rằng bạn sử dụng câu lệnh Break để thoát vòng lặp tại một số điểm.press CTRL+C . When writing infinite loops, make sure you use the break statement to exit the loop at some point.