Critiquing my Combat Sequence

Hi all,

I feel as though I finally have a good grasp on OOP, or at least good enough to move on from Exercise 43. :sweat_smile:

There shouldn’t be any glaring bugs - and if there are, they shouldn’t be detrimental to the program running. I mainly wanted to post my code here to get some critiques. I was able to get it running, and I feel the wiser now. At the same time, just looking at it, I know some things could have been done more optimally. I plan on revisiting this script once I get to Exercise 45, where we design our own game, to see what improvements I could personally make.

(Hopefully, it’s acceptable to post this here. I can remove it or post this in a different category if need be.)


Not bad, so the only thing I see is this:

                        print("The gorthon succesfully defends your attack.")
                    print("The player attacks, the gorthon doesn't defend.")
                    print(f"You inflicted {player_attack} damage on the gorthon")
           -= player_attack
                    print(f"Gorthon has {} health.")
            elif player_choice == 3:
                print("The player is healing.")
                if self.combatant1.potions > 0:
           += self.combatant1.healing()
                    print(f"The player now has {}.")
                    self.combatant1.potions -= 1
                    print(f"The player now has {self.combatant2.potions} potions left.")
                elif self.combatant1.potions == 0:
                    print("Player doesn't have enough potions left!")

Ok, so when have else and some code inside the else, then don’t put a pass. And if you ever just put an else followed by a pass then most likely one of your elif clauses should be an else OR you need to add an assert so that you watch if that condition has every been triggered:

if do good thing:
   assert False, "This should not run."

That’ll help you find bugs in your logic.

Also, there is one situation I forgot to add to my rule of “every if has an else”, in the situation where you’re just looping through a list of things and collecting them you can drop the else:

for x in [1,2,3]:
    if x > 1:
        # do something

There’s no need to have an else there as it’s a common pattern to filter things out like that and the else is just confusing.

1 Like

Alright. I fixed all of that. Admittedly, one of the more unexpected things I’ve encountered in Python is that it’s very easy to get lost in the logic of the program you’re writing. If-else statements are oddly tricky.


I’d say that’s not really a Python thing but more just general code problem, and actually probably the central thing you deal with as a programmer.

1 Like