🌐
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.
🌐
Runestone Academy
runestone.academy › ns › books › published › py4e-int › iterations › infinite_loops.html
6.3. Infinite loops — Python for Everybody - Interactive
This loop is obviously an infinite loop because the logical expression on the while statement is simply the logical constant True: ... As you can see above, the Code Lens gives you a warning because it runs for over 1000 steps. If you make the mistake of running this code, you will learn quickly how to stop a runaway Python ...
Discussions

"for i in range()" to do an infinite loop with a counter - Ideas - Discussions on Python.org
Hi, Usually in Python we can avoid the i = 0 … i += 1 paradigm that we use in other languages when we need to count things, thanks to enumerate(...), for i in range(100), etc. Along the years I have nearly always found a more “pythonic” replacement for code containing i = 0 … i += 1. ... More on discuss.python.org
🌐 discuss.python.org
4
August 10, 2022
Infinite While Loop
Hey again everybody! Long time no see! Working on text input and outputs right now. My current code is supposed to count each letter that appears in a text document (“lyrics.txt”) and output the number of times each letter appears. The gets stuck in an infinite while loop though, and I’m ... More on discuss.python.org
🌐 discuss.python.org
0
0
November 24, 2021
python - infinite while loop although I put break statement - Stack Overflow
So it will never print hit point, ... the while loop when l==r, either it will never be l==r because of precision and loop infinite, so in both situations if condition never match. And comparing a floating value to break a loop is not ideal. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s... ... 2 Why does my python code produce ... More on stackoverflow.com
🌐 stackoverflow.com
[Thought experiment] Achieving one-line infinite loops in Python
One-liner infinite print loop, that doesn't use while, append, etc.: for i in __import__('itertools').count(): print(i) More on reddit.com
🌐 r/learnpython
15
5
September 5, 2018
People also ask

Can a while loop run infinitely in Python? How can I avoid infinite loops?
Yes, a while loop can run infinitely when the condition mentioned in the loop structure will always be true. In order to avoid these infinite loops, you can use break statements.
🌐
intellipaat.com
intellipaat.com › home › blog › python while loop
Python While Loop: Explained with While Loop Flowchart
What is a Python while loop, and when can I use it?
A while loop in Python is a concept or tool in which you write the particular condition that you want to execute repeatedly and it will get executed until the condition remains true. You can use the while loops when you do not know the exact number of repetitions of a particular problem
🌐
intellipaat.com
intellipaat.com › home › blog › python while loop
Python While Loop: Explained with While Loop Flowchart
Can I use else with a while loop in Python?
Yes, you can use else statements with a while loop in Python. Basically, the else statement will work only when the while loop ends naturally and not with a break statement.
🌐
intellipaat.com
intellipaat.com › home › blog › python while loop
Python While Loop: Explained with While Loop Flowchart
🌐
Unstop
unstop.com › home › blog › python infinite loop | types, applications & more (+examples)
Python Infinite Loop | Types, Applications & More (+Examples)
October 25, 2024 - In this scenario, the chatbot continuously interacts with users, responding to their inputs until the user decides to end the conversation. In Python, you can create an infinite loop using a while loop with the condition set to True.
🌐
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
In Python, a while loop is used to execute a block of statements repeatedly until a given condition is satisfied. When the condition becomes false, the line immediately after the loop in the program is executed. In below code, loop runs as long as the condition cnt < 3 is true. It increments the counter by 1 on each iteration and prints "Hello Geek" three times. ... If we want a block of code to execute infinite number of times then we can use the while loop in Python to do so.
Published   4 days ago
🌐
freeCodeCamp
freecodecamp.org › news › python-while-loop-tutorial
Python While Loop Tutorial – While True Syntax Examples and Infinite Loops
November 13, 2020 - This is one possible solution, incrementing the value of i by 2 on every iteration: i = 5 while i < 15: print("Hello, World!") # Update the value of i i += 2 · Great. Now you know how to fix infinite loops caused by a bug.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Python while Loop (Infinite Loop, break, continue) | note.nkmk.me
August 18, 2023 - If the condition in the while statement is always True, the loop will never end, and execution will repeat infinitely. In the following example, the Unix time is acquired using time.time(). The elapsed time is then measured and used to set the ...
🌐
W3Schools
w3schools.com › python › python_while_loops.asp
Python While Loops
With the break statement we can stop the loop even if the while condition is true: ... Note: The else block will NOT be executed if the loop is stopped by a break statement. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
Find elsewhere
🌐
Gitbooks
buzzcoder.gitbooks.io › codecraft-python › content › while-loop › infinite-loop-and-break.html
Infinite loops and break · CodeCraft-Python - BuzzCoder
This can be done with break keyword. ... Here is a good example of an infinite loop that works: while True: n = int(input('Give me an integer: ')) if n == 0: break print(str(n) + '*' + str(n) + '=' + str(n*n)) print('done')...
🌐
Scaler
scaler.com › home › topics › what is infinite loop in python?
What is Infinite Loop in Python? | Scaler Topics
May 8, 2024 - A fake infinite loop is a kind ... the loop. For example, we are given a program in which we need to print all the even multiples of 3 that is less than and equal to a given number n....
🌐
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu › notebooks › chapter05.02-While-Loops.html
While Loops — Python Numerical Methods
Python computes n >= 1 or 0.5 >= 1, which is false so the while-loop ends with i = 4. You may have asked, “What if the logical expression is true and never changes?” and this is indeed a very good question. If the logical expression is true, and nothing in the while-loop code changes the expression, then the result is known as an infinite loop. Infinite loops run forever, or until your computer breaks or runs out of memory. EXAMPLE...
🌐
StudySmarter
studysmarter.co.uk › computer science › computer programming › python infinite loop
Python Infinite Loop: Creation, Example & Error | StudySmarter
August 10, 2023 - To create an infinite loop in Python, use the `while` statement with a condition that always evaluates to `True`. For example: ```python while True: print("This is an infinite loop!") ``` This loop will continue executing the `print` statement indefinitely until the program is forcibly terminated ...
🌐
Tutorialspoint
tutorialspoint.com › python › python_while_loops.htm
Python while Loop Statements
April 4, 2023 - The following example shows how to use one-line while clause. flag = 0 while (flag): print ("Given flag is really true!") print ("Good bye!") When you run this code, it will display the following output − ... Change the flag value to "1" and ...
🌐
TutorialKart
tutorialkart.com › python › python-while-loop › python-infinite-while-loop
Flowchart – Python Infinite While Loop
November 30, 2020 - So, considering these two statements, we can provide the boolean value True, in place of condition, and the result is a infinite while loop. ... Note: You will see the string hello print to the console infinitely. To interrupt the execution of the program, enter Ctrl+C from keyboard. This generates KeyboardInterrupt and the program will stop. Instead of giving True boolean value for the condition, you can also give a condition that always evaluates to True. For example, the condition 1 == 1 is always true.
🌐
Programiz
programiz.com › python-programming › looping-technique
Python Looping Techniques
It can be implemented using an infinite loop along with a conditional break at the end. This is similar to the do...while loop in C. # Python program to illustrate a loop with the condition at the bottom # Roll a dice until the user chooses to exit # import random module import random while True: input("Press enter to roll the dice") # get a number between 1 to 6 num = random.randint(1,6) print("You got",num) option = input("Roll again?(y/n) ") # condition if option == 'n': break
🌐
Python.org
discuss.python.org › ideas
"for i in range()" to do an infinite loop with a counter - Ideas - Discussions on Python.org
August 10, 2022 - Hi, Usually in Python we can avoid the i = 0 … i += 1 paradigm that we use in other languages when we need to count things, thanks to enumerate(...), for i in range(100), etc. Along the years I have nearly always found a more “pythonic” replacement for code containing i = 0 … i += 1. There is an exception with this code: an infinite loop with a counter: i = 0 while True: ... if breaking_condition: break i += 1 Proposal: could we accept that range() without any parameter ...
🌐
Programiz
programiz.com › python-programming › while-loop
Python while Loop (With Examples)
If the condition of a while loop always evaluates to True, the loop runs continuously, forming an infinite while loop. For example,
🌐
Python.org
discuss.python.org › python help
Infinite While Loop - Python Help - Discussions on Python.org
November 24, 2021 - Hey again everybody! Long time no see! Working on text input and outputs right now. My current code is supposed to count each letter that appears in a text document (“lyrics.txt”) and output the number of times each letter appears. The gets stuck in an infinite while loop though, and I’m ...
🌐
Scientech Easy
scientecheasy.com › home › blog › infinite loop in python
Infinite Loop in Python - Scientech Easy
January 25, 2026 - It will stop the loop, come out of loop and execute the next statement that is following the infinite loop. Let’s take an example program based on infinite while loop where we will print the sum of squares of numbers from 1 to 20.
Top answer
1 of 4
1

Everyone here seems to be adamant on using some fancy tricks to make floating comparison works. Why not just multiply it all by 10 and get rid of floats altogether? :-)

I don't know if it is the fastest solution but it should have less corner cases.

i = 0
while True: # <- condition removed to allow the "hit point" if to work

    print(r, i / 10)
    if (i == r * 10):
        print("\nhit piont: ", i / 10)
        break

    if (sub_m > 0 and sub_b > 0):
        i -= 1
    elif (sub_m < 0 and sub_b < 0):
        i -= 1
    else:
        i += 1
2 of 4
0

Running this in my debugger showed that you're getting floating point representation errors. This means that although technically you should be getting numbers perfectly rounded to 1 decimal given that you're applying increments of 0.1, in reality this isn't the case:

As you can see, r = -2.0 and i = -2.00...4, thus at no point is r == i.

You can fix this by adding another round statement at the end:

print("enter the first m: ")
m = input()  # m = slope

print("enter the first b: ")
b = input()  # b = y-intercept

print("enter the second m: ")
m1 = input()

print("enter the second b: ")
b1 = input()

sub_m = int(m) - int(m1) #sub = subtract
sub_b = int(b) - int(b1)

if (sub_m == 0):
    print("parallel")

x = float(-sub_b / sub_m)
r = round(x, 1)

i = 0.0
while i != r:

    print(r, i)

    if (sub_m > 0 and sub_b > 0):
        i -= 0.1
    elif (sub_m < 0 and sub_b < 0):
        i -= 0.1
    else:
        i += 0.1
    i = round(i, 1)  # <- this

print(f"Hit pt: {i}")

HOWEVER: This is still error prone, and I recommend finding a way to avoid if i==r altogether in the code. If i is lower than r, exit the loop when it finally becomes bigger, and viceversa. Its best practice to avoid using the == condition when comparing floats, and to find a way to use <= and >=.

🌐
Intellipaat
intellipaat.com › home › blog › python while loop
Python While Loop: Explained with While Loop Flowchart
October 14, 2025 - If we run the above code block, it will execute an infinite loop that will ask for our names again and again. The loop won’t break until we press ‘Ctrl+C’. Python doesn’t have a do-while loop. But we can create a program to implement do-while. It is used to check conditions after executing the statement.