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
Answer from Piotr Siupa on Stack Overflow
🌐
Real Python
realpython.com › python-while-loop
Python while Loops: Repeating Tasks Conditionally – Real Python
March 3, 2025 - You prevent an infinite loop by ensuring that the loop’s condition will eventually become false through proper logic in the loop condition and body. What is the purpose of the break statement in a while loop?Show/Hide · You use the break statement to immediately exit a while loop, regardless of the loop’s condition. Can you use an else clause with a while loop in Python?Show/Hide
🌐
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
Discussions

python - infinite while loop although I put break statement - Stack Overflow
this code is supposed to take slope (m) and y-intercept (b) of two lines and checks if these two line hit each other or not. the problem is my while loop is infinite although I have condition and b... 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
Need help understanding this while loop
I’m new to while loops, and I don’t quite get what’s going on in this code from my book: current_number = 1 while current_number More on discuss.python.org
🌐 discuss.python.org
0
February 13, 2024
Unable to quit infinite while loop in idle 3.12.0 (64 bit) with keyboard interrupt
Hello I am learning python, in IDLE when i write # infinite while loop i = 1 while i < 2: print(i) it produces an infinite while loop . But for some reason I am unable to use the keyboard interrupt (ctrl + C) Id… More on discuss.python.org
🌐 discuss.python.org
0
December 10, 2023
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
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
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
🌐
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
... Note: It is suggested not to ... Python programming language allows to use one loop inside another loop which is called nested loop....
Published   1 month ago
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 >=.

🌐
Unstop
unstop.com › home › blog › python infinite loop | types, applications & more (+examples)
Python Infinite Loop | Types, Applications & More (+Examples)
October 25, 2024 - An infinite loop occurs when a sequence of instructions continues to execute endlessly without a defined endpoint, often due to the loop's condition being perpetually met. While this might sound problematic, ...
🌐
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.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-while-loop
Python While Loop - GeeksforGeeks
Therefore, the body of the loop is run infinite times until the memory is full. ... Loop control statements change execution from their normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are ...
Published   December 23, 2025
🌐
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu › notebooks › chapter05.02-While-Loops.html
While Loops — Python Numerical Methods
Python computes n >= 1 or 0.5 >= ... 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 ......
🌐
Medium
medium.com › @tiaraoluwanimi › python-while-loops6420e2f913a3-6420e2f913a3
Python While Loops: Common Errors & How to Fix Them
November 10, 2021 - If you do not use the same indent on the multiplier line, it’ll result in an infinite loop. The fix is highlighted in the following code snippet. def times_table(number): # Initialize the starting point of the multiplication table multiplier = 1 # Only want to loop through 5 while multiplier <= 5: result = number * multiplier print(str(number) + "x" + str(multiplier) + "=" + str(result)) # Increment the variable for the loop multiplier += 1 times_table(3)
🌐
Reddit
reddit.com › r/learnpython › [thought experiment] achieving one-line infinite loops in python
r/learnpython on Reddit: [Thought experiment] Achieving one-line infinite loops in Python
September 5, 2018 -

DISCLAIMER: This post is mainly just curious thoughts, it has nothing to do with real-life application or good practice. So please don't actually use any examples provided.

Python is (or at least was) rather famous for its possibilities for one-liners (programs occupying only a single line of code) some time ago. A lot of things can be achieved like this, but among the most puzzling things must be infinite loops; they aren't exactly easy to implement with the tools we have available.

An infinite loop usually requires the use of a while-loop, because for-loops have a beginning and an end. Using a while-loop in one-liners is problematic, though, because you may only use it once, on the top level. This is due to how Python restricts block structures to either be separated by whitespace (and proper indentation), or to only have a single depth level following it. In other words,

while True: print("This works!")

is valid Python, but

while True: if 1 == 1: print("But this doesn't...)

is not.

We do have another "kind" of loop, though; list comprehensions. They are unique in that they may be nested as we see fit, all while using only a single line.

[["Order pizza." for _ in range(6)] for _ in range(42)]

But this doesn't give us an infinite loop; even if we simply input a ridiculously large number to range, it's still technically finite no matter what kind of hardware we're using. Thus, a different approach is required. I mentioned how infinite loops usually require the use of while-loops in Python. We can, however, utilise a certain property of Python to create an infinite loop with for-loops.

nums = [1, 2, 3, 4]
for num in nums:
    print(num)

Okay, that prints out four numbers. Not exactly infinite. But if we tweak our approach a little...

nums = [1]
for num in nums:
    print(num)
    nums.append(num + 1)

We actually get... as many numbers as the computer's memory allows. With this, we can essentially get something like this to work:

nums=[1];[(print(num) and nums.append(num+1)) for num in nums]

(Disclaimer; I never tested if that actually runs.)

It's not a pure one-liner, because it still technically requires two lines (fused together with a semicolon), but it's a proof-of-concept. I initially tried to make it work without having to define a variable, but failed to find a way.

I hope this was mildly interesting, I don't usually write stuff like this. Just found it curious myself, so why not share the thought? Maybe someone can even improve on this.

🌐
Scaler
scaler.com › home › topics › what is infinite loop in python?
What is Infinite Loop in Python? | Scaler Topics
May 8, 2024 - We have mainly three types of infinite loops in Python. They are: ... Let us discuss them one by one briefly. A fake infinite loop is a kind of loop that looks like an infinite loop (like while True: or while 1:) but has some certain condition that will terminate the loop.
🌐
Python.org
discuss.python.org › python help
Need help understanding this while loop - Python Help - Discussions on Python.org
February 13, 2024 - I’m new to while loops, and I don’t quite get what’s going on in this code from my book: current_number = 1 while current_number <= 5: print(current_number) current_number += 1 After just watching a video on Yo…
🌐
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 ...
🌐
Python.org
discuss.python.org › python help
Unable to quit infinite while loop in idle 3.12.0 (64 bit) with keyboard interrupt - Python Help - Discussions on Python.org
December 10, 2023 - Hello I am learning python, in IDLE when i write # infinite while loop i = 1 while i < 2: print(i) it produces an infinite while loop . But for some reason I am unable to use the keyboard interrupt (ctrl + C) Id…
🌐
Python Morsels
pythonmorsels.com › while-loops
Python's "while" loop - Python Morsels
June 20, 2025 - This loop would never end because we forgot to increment our count variable in the body of our loop! A loop that goes on forever is called an infinite loop. Be careful not to accidentally make infinite loops in Python.
🌐
StudySmarter
studysmarter.co.uk › computer science › computer programming › python infinite loop
Python Infinite Loop: Definition & Examples
August 10, 2023 - In Python, an infinite loop can be created using 'while' or 'for' loop structures, with an appropriate condition or iterator that never reaches its stopping point.A Python infinite loop has both advantages and disadvantages, which should be carefully considered before implementing one.
🌐
Python.org
discuss.python.org › python help
Infinte while loop using try/except - Python Help - Discussions on Python.org
June 15, 2024 - hey y’all! I’m taking an online class called Cs50P, I thought I practiced using while loops + try/exceptions to the point of being confident using this structure. I’m running into an infinite loop when the exceptions are raised in the convert() function. I just don’t understand why.
🌐
Intellipaat
intellipaat.com › home › blog › python while loop
Python While Loop: Explained with While Loop Flowchart
October 14, 2025 - Once counter >= 10, the program exits the while loop and continues with any code after it. Get 100% Hike! Master Most in Demand Skills Now! An infinite while loop refers to a while loop where the while condition never becomes false.
🌐
Runestone Academy
runestone.academy › ns › books › published › fopp › MoreAboutIteration › WPInfiniteLoops.html
14.6. 👩‍💻 Infinite Loops — Foundations of Python Programming
First off, if the variable that you are using to determine if the while loop should continue is never reset inside the while loop, then your code will have an infinite loop.
🌐
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 not entirely sure why.