just indent your code correctly:

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

You need to understand that the break statement in your example will exit the infinite loop you've created with while True. So when the break condition is True, the program will quit the infinite loop and continue to the next indented block. Since there is no following block in your code, the function ends and don't return anything. So I've fixed your code by replacing the break statement by a return statement.

Following your idea to use an infinite loop, this is the best way to write it:

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
Answer from Mapad on Stack Overflow
๐ŸŒ
Real Python
realpython.com โ€บ python-while-loop
Python while Loops: Repeating Tasks Conditionally โ€“ Real Python
March 3, 2025 - In this tutorial, you'll learn about indefinite iteration using the Python while loop. You'll be able to construct basic and complex while loops, interrupt loop execution with break and continue, use the else clause with a while loop, and deal with infinite loops.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_while_loops.asp
Python While Loops
The while loop requires relevant variables to be ready, in this example we need to define an indexing variable, i, which we set to 1. With the break statement we can stop the loop even if the while condition is true:
๐ŸŒ
Enki
enki.com โ€บ post โ€บ how-to-exit-a-loop-in-python
Enki | Blog - How to Exit a Loop in Python
Learn to manage loops effectively in Python using the break statement to exit for and while loops under specific conditions, optimizing code efficiency.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ my while loop won't stop, how do i end it?
r/learnpython on Reddit: My while loop won't stop, how do I end it?
February 16, 2024 -

No matter where I put the break or โ€œreturn,โ€ it just wonโ€™t work. I need it to end then go onto the next defined function (printInvoice) but it either continues on an endless loop or ends but wonโ€™t go onto the next function.

Here's the code:

Dictionary1 = {'Brad': 18.99, 'Chad': 16.99, 'Andy': 15.99, 'Suzy': 12}

Shipping = {'Ally Express': [10.00, 0.50], 'DHL Express': [11.75, .35], 'Bob Economy': [10.00,40], 'Queen Priority':[13.00, 0.40]} 
GST = 0.05

def checkInput(): 
    order = [] 
    while True: 
    for value in Dictionary1:
        try: decision = int(input("How many sets of " + value + " do you want?"))
            order.append(str(decision)) 
        except ValueError: 
            print("You did not enter an integer") 
            decision = int(input("How many sets of " + value + " do you want?"))
    break 

def printInvoice(order): 
print("We can deliver by Ally Express, Bob Economy or Queen Priority Shipping")
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Can't get while loop to stop - Python Help - Discussions on Python.org
May 6, 2024 - I am making a number guessing game, and I have a while loop over the whole thing. The problem is, I canโ€™t get it to stop when I want it to. import random import sys import time ynChoice = 'x' global runGTN runGTN = True while runGTN == True: #the loop typing_speed = 300 #wpm def printSlow(*t): for l in t: sys.stdout.write(l) sys.stdout.flush() time.sleep(random.random()*10.0/typing_speed) print() def confirmChoice(ynChoice): ...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-use-break-continue-and-pass-statements-when-working-with-loops-in-python-3
How To Use break, continue, and pass Statements in Python | DigitalOcean
April 24, 2026 - For instance, you might encounter ... for future code. Python provides three powerful statements to handle these cases: break, continue, and pass. The break statement allows you to exit a loop entirely when a specific condition is met, effectively stopping the loop ...
Find elsewhere
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ how to stop a while loop in python
How to Stop a While Loop in Python - Be on the Right Side of Change
September 28, 2022 - You can also watch my explainer video as you go through the article: The most Pythonic way to end a while loop is to use the while condition that follows immediately after the keyword while and before the colon such as while <condition>: <body>. ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-kill-a-while-loop-with-a-keystroke-in-python
How to Kill a While Loop with a Keystroke in Python? - GeeksforGeeks
July 23, 2025 - import time try: num = 11 while True: if num % 2 == 0: break print(num) num = num + 2 time.sleep(1) # Wait 1 second before next iteration except KeyboardInterrupt: pass print("Continuing with the program") ... This approach allows the user to press a specific key (like q) to exit the loop. But to implement this in our code we need to first install the keyboard library using this command: ... import keyboard def main_loop(): while True: print("Working...") # Press 'q' to exit if keyboard.is_pressed('q'): print("Loop terminated by user.") break if __name__ == "__main__": main_loop()
๐ŸŒ
Maschituts
maschituts.com โ€บ exit-while-loops-in-python
How to Exit While Loops in Python โ€” 4 Best Ways
September 30, 2023 - The above code will run forever. As already mentioned, we want to avoid that. Therefore, we need to force the while loop to terminate prematurely. There are three ways to do that. The break statement stops the execution of a while loop.
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to Stop a While Loop in Python? - YouTube
Full Tutorial: https://blog.finxter.com/how-to-stop-a-while-loop-in-python/Email Academy + Keywords Cheat Sheet: https://blog.finxter.com/email-academy/โ–บโ–บ Do...
Published ย  May 5, 2021
๐ŸŒ
Team Treehouse
teamtreehouse.com โ€บ community โ€บ how-to-stop-the-while-loop
how to stop the while loop (Example) | Treehouse Community
October 29, 2017 - ... def disemvowel(word): while ... word.remove("U") except ValueError: pass return word ... The python 'break' statement is used to break out of a loop....
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-while-loop-tutorial
Python While Loop Tutorial โ€“ While True Syntax Examples and Infinite Loops
November 13, 2020 - >>> while True: print(0) 0 0 0 0 0 0 0 0 0 0 0 0 0 Traceback (most recent call last): File "<pyshell#2>", line 2, in <module> print(0) KeyboardInterrupt ยท The loop runs until CTRL + C is pressed, but Python also has a break statement that we ...
๐ŸŒ
Pierian Training
pieriantraining.com โ€บ home โ€บ python tutorial: how to stop an infinite loop in python
Python Tutorial: How to stop an infinite loop in Python - Pierian Training
April 12, 2023 - If you have written a function that contains an infinite loop, you can use the return statement to exit the function and stop the loop. def my_function(): while True: # Your code here if condition: return
๐ŸŒ
PythonHow
pythonhow.com โ€บ how โ€บ break-a-while-loop
Here is how to break a while loop in Python
New: Practice Python, JavaScript & SQL with AI feedback โ€” Try ActiveSkill free โ†’ ร— ... import random # This loop will run forever unless we break it while True: # Generate a random int between 1 and 10 random_integer = random.randint(1, 10) print(random_integer) # Stop the loop if the random int is 5 if random_integer == 5: break
๐ŸŒ
Coursera
coursera.org โ€บ tutorials โ€บ python-break
How to Use Python Break | Coursera
When working with loops, remember that indentation tells Python which statements are inside the loop and which are outside the loop. Your break statement should follow your if statement and be indented.