I'm quite new to python and I want to change this piece of code from one line if statement into multiple lines.
The code:
self.a = -self.ax if self.a <= 0 or self.a >= dd.maxX else self.ax
self.b = -self.bx if self.b <= 0 or self.b >= dd.maxX else self.bx
What I did so far was this:
if self.a <= 0 or self.a >= dd.maxX:
self.a = -self.ax
if self.b <= 0 or self.b >= dd.maxX:
self.a = -self.bx
but when I add else statment the code doesn't work anymore. How to fix this problem?
CodePudding user response:
The original code assigns a value if the test is true and a different value if the test is not true:
Perhaps this makes it more visible:
self.a = (
-self.ax if self.a <= 0 or self.a >= dd.maxX
else self.ax
)
You need to account for both cases as well:
if self.a <= 0 or self.a >= dd.maxX:
self.a = -self.ax
else:
self.a = self.ax
