1

In the following code I would like the CHAC to show an empty string so that I can concatenate it with similar strings later.

def __init__(self, **kwargs):
    self.id = random.randint(self.min_hit_points, self.max_hit_points)
    self.level = random.randint(self.min_level, self.max_level)
    self.CHAC = str(int(self.id / 5 - 1 * (self.level - 1))) 
    self.color = random.choice(COLORS)
1
  • The easy way is self.CHAC = "" if self.CHAC=='0' else self.CHAC Commented Aug 12, 2021 at 23:13

2 Answers 2

2

Since integer zero is falsy, you can do

str(int(self.id / 5 - 1 * (self.level - 1)) or '')
Sign up to request clarification or add additional context in comments.

Comments

0

You can use a conditional expression:

value = int(self.id/5-1*(self.level-1))
self.CHAC = '' if value == 0 else str(value)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.