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
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-while-loop
Python While Loop - GeeksforGeeks
Here, the condition for while will be True as long as the counter variable (count) is less than 3. ... An infinite loop is a loop that keeps running continuously because its condition always remains True.
Published   June 3, 2026
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
python - Ending an infinite while loop - Stack Overflow
I currently have code that basically runs an infinite while loop to collect data from users. Constantly updating dictionaries/lists based on the contents of a text file. For reference: while (True... More on stackoverflow.com
🌐 stackoverflow.com
Infinite While Loop
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. Thoughts? More on discuss.python.org
🌐 discuss.python.org
10
0
November 24, 2021
Why am i getting an infinite loop when not using a while loop?
Well, you keep adding stuff to the list, how would it ever end? More on reddit.com
🌐 r/learnprogramming
40
84
April 15, 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
🌐
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 ...
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 >=.

🌐
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 ...
🌐
freeCodeCamp
freecodecamp.org › news › python-while-loop-tutorial
Python While Loop Tutorial – While True Syntax Examples and Infinite Loops
November 13, 2020 - When you write a while loop, you ... eventually stop. An infinite loop is a loop that runs indefinitely and it only stops with external intervention or when a break statement is found....
Find elsewhere
🌐
TutorialKart
tutorialkart.com › python › python-while-loop › python-infinite-while-loop
Flowchart – Python Infinite While Loop
November 30, 2020 - As the condition is never going to be False, the control never comes out of the loop, and forms an Infinite Loop as shown in the above diagram. Firstly, we know that the condition in while statement has to always evaluate to True for it to become infinite Loop.
🌐
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, ...
🌐
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. break will cause the current loop to end, and the computer will jump to the code directly following the loop. 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) + '*' + ...
🌐
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu › notebooks › chapter05.02-While-Loops.html
While Loops — Python Numerical Methods
Python computes n >= 1 or 0.5 >= ... 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....
🌐
Python.org
discuss.python.org › python help
Infinite While Loop - Python Help - Discussions on Python.org
November 24, 2021 - 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. Thoughts?
🌐
StudySmarter
studysmarter.co.uk › python infinite loop
Python Infinite Loop: Definition & Examples | StudySmarter
An infinite loop in Python is a loop that continues to execute indefinitely due to the loop's condition always evaluating as true, such as "while True:" or a loop missing a terminating condition. This can be useful for programs or processes that need to run continuously until externally ...
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › python infinite loop
Python Infinite Loop | Top 4 Types of Statements in Python Infinite Loop
May 10, 2024 - Loops are compelling and essential, but an infinite loop is the only pitfall. Python has two types of loops, only ‘While loop’ and ‘For loop.’ While loop works precisely as the IF statement but in the IF statement, we run the block of code just once, whereas, in a while loop, we jump back to the same point from where the code began.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Programiz
programiz.com › python-programming › looping-technique
Python Looping Techniques
# Program to illustrate a loop with the condition at the top # Try different numbers n = 10 # Uncomment to get user input #n = int(input("Enter n: ")) # initialize sum and counter sum = 0 i = 1 while i <= n: sum = sum + i i = i+1 # update counter # print the sum print("The sum is",sum) ... This kind of loop can be implemented using an infinite loop along with a conditional break in between the body of the loop.
🌐
The Python Coding Stack
thepythoncodingstack.com › the python coding stack › to infinity and beyond • the infinite `for` loop
To Infinity and Beyond • The Infinite `for` Loop
June 25, 2024 - The obvious conclusion is that if you want an infinite loop, you'll need to use the while loop—while True usually does the trick. But you can also use a for loop to create an infinite loop. Let's explore further. I'll keep today's article short, partly because I'm in the final stages of completing The Python Coding Book, which is taking up most of my time this week.
🌐
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.
🌐
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.
🌐
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.