while loop never ends and I can’t figure out how to stop it

Basically, I have an assignment, everything is going smoothly, but when I input a certain value into the loop it never stops, unlike other values that I input, which is odd and I can't figure out how to deal with it. Here is the code:

dim = int(input())
pos = int(input())
v = int(input())
vi = v
a = int(input())
posa = dim +1

print(pos)
while v != 0 or v*vi>0:
    if -dim < pos < dim or pos == dim or pos == -dim:
        pos += v
    elif pos > dim:
        pos = dim
        v -= a
        v = -v
    elif pos < -dim:
        pos = -dim
        v += a
        v = -v
    if -dim <= pos <= dim and not posa==pos:
        print(pos)
        posa = pos

For some context, it's a program that is meant to give the position of a bumper car, dim and -dim are the walls the car can crash into and then bounce back, pos is the starting position, v is the initial velocity and a is by how much the absolute value of the value lowers once bumps into a wall. It is supposed to stop eventually once there is no more velocity. Although, problem is, if the input is 10,-3,6,2; it works perfectly, but if it input is 10,-3,8,3; the loop never ends, and I can't figure out why.

EDIT: I can't use for, def, import, break, continue and lists and similar things, basically just the most basic stuff like if and while.



Read more here: https://stackoverflow.com/questions/64950285/while-loop-never-ends-and-i-cant-figure-out-how-to-stop-it

Content Attribution

This content was originally published by João Pedro at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: