Creating a leveling up method in OOP

I’m currently working through the CS career path module and I’m at the section in OOP where I create a game.

One of my classes is Fighter, and one of the attributes is Rank. I’m trying to write this code so that the attribute Rank is displayed as a string (like belts in martial arts i.e. white, blue, purple, etc.).

I want one of my methods to be something along the lines of gets_promoted where the selected object(fighter) would be advanced to the next rank(white to blue, blue to purple, and so on).

I’m sure the answer is staring me in the face, but I’m stuck. I tried something where I used operational if/elif statements where if input_rank == 1: print(“white”) but that’s obviously missing something.

def gets_promoted(self):
input_rank == 0
if input_rank == 1:
elif input_rank == 2:
elif input_rank == 3:
elif input_rank == 4:
self.rank = self.rank + 1
print("{name} got promoted to {rank} belt!".format(name =, rank = self.rank))

Any advice would be greatly appreciated!

Your code, formatted with appropriate indentations:

You don’t seem to set the value for input_rank anywhere in this function.

Is the printing of the color supposed to happen before or after the rank is increased?

Don’t forget to use the </> button and put the code between ``` and ``` when pasting your code to keep the formatting.

Maybe assigning a variable would be more appropriate than printing the color immediately.

def gets_promoted(self):

  self.rank = self.rank + 1  # rank is increased
  input_rank = self.rank 

  color = "clear"
  if input_rank == 1:
    color = "white"
  elif input_rank == 2:
    color = "blue"
  elif input_rank == 3:
    color = "purple"
  elif input_rank == 4:
    color = "brown"
    color = "black"
  print("{name} got promoted to {rank} belt!".format(name =, rank = color))
  return color
  # end of gets_promoted method

(You can also use a list or dictionary instead of some of the if/elif/else stuff.)

1 Like

Maybe using a list would be more appropriate without all the logical excess. Think about it. The random numbers are in the same range as the indices. Easy peasy.

1 Like

I figured something along these lines but honestly need to refresh my memory on the concept. I’m thinking I bit off more than I can chew, here. I’ll spend some time studying and practicing. Thanks

1 Like

Start with a list of colors. ‘clear’ can be at the start of the list with index 0.

def __init(self, name, rank=0): = name
  self.rank = rank
  self.colors = ['clear', 'white', 'blue', 'purple', 'brown', 'black']
def gets_promoted(self)
  self.rank += 1
  print (f"{] got promoted to {self.colors[self.rank] belt!")

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.