2

I have a class with two integer attributes, _xp and level. I have a while loop which compares these two to make sure they're both positive:

while self.level > 0 and self._xp < 0:
    self.level -= 1
    self._xp += self.get_xp_quota()

My PyCharm claims this can be simplified:

Simplify chained comparison

Can it really? I want to make sure before reporting a bug to PyCharm.

I also found a similar question but in that case the two variables were the same, mine has two different attributes.

Community
  • 1
  • 1
Markus Meskanen
  • 19,939
  • 18
  • 80
  • 119

1 Answers1

5

IIRC, you could rewrite this as:

while self._xp < 0 < self.level:
    self.level -= 1
    self._xp += self.get_xp_quota()

as per your reference above. It doesn't really matter that there's 2 different attributes or the same variable, ultimately you are simply comparing the values of each.

Let me know if that works.

Vlad B
  • 323
  • 3
  • 11