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 Overflowpython - infinite while loop although I put break statement - Stack Overflow
[Thought experiment] Achieving one-line infinite loops in Python
Need help understanding this while loop
Unable to quit infinite while loop in idle 3.12.0 (64 bit) with keyboard interrupt
Can a while loop run infinitely in Python? How can I avoid infinite loops?
Can I use else with a while loop in Python?
What is a Python while loop, and when can I use it?
Videos
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
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 >=.
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.