I wanted to see if anyone would like to review the existing code I have for a beginner program that simulates a battle. I have learned a heck of a lot while doing it, and I had a few questions regarding some things that I found worked, but I am not sure on the why of them working.

Please do note that this is not finished and I am definitely going to see this program through to the end with a few more features that what I have now. However, at this stage it is functional and I wanted to see if the things I am doing now will lead to a functional program at the end.

The main issues I have had so far come from sticking the dictionaries inside of a list. I had to deal with a lot of errors passing a dictionary function to a list and vice versa.

Here is the code:

    # A combat simulator which allows input for your name, your weapon, randomly chooses your enemy, and simulates a battle based on simple stats such as HP and dmg.
    # a dice roll will modify dmg for things such as ambushes and counter attacks. <- Maybe!
    # TODO: X implement dictionary (?) for stats along with enemy list and weapon list, input validation, don't submit to feature creep!, X verify return variable from
    # local scope, battle sequence, end program
    # 6/5 - Plan to implement: Final scripted battle sequence to simulate one battle, will need to understand how to modify the HP value accordingly...

    import random, time

    enemyOrc = {"Name" : "Orc", "HP" : "90", "Attack" : "18"}
    enemyGK = {"Name" : "Goblin King", "HP" : "182", "Attack" : "16"}
    enemyGR = {"Name" : "Giant Rat", "HP" : "32", "Attack" : "3"}
    enemyWitch = {"Name" : "Witch", "HP" : "68", "Attack" : "32"}
    enemyEM = {"Name" : "Evil Mirror", "HP" : "350", "Attack" : "11"}

    weaponSword = {"Name" : "Sword", "Damage" : "12"}
    weaponAxe = {"Name" : "Axe", "Damage" : "18"}
    weaponQS = {"Name" : "Quarter-Staff", "Damage" : "22"}
    weaponDagger = {"Name" : "Dagger", "Damage" : "8"}
    weaponFists = {"Name" : "Fists", "Damage" : "28"}

    enemyList = [enemyOrc, enemyGK, enemyGR, enemyWitch, enemyEM]
    weaponList = [weaponSword, weaponAxe, weaponQS, weaponDagger, weaponFists]

    def introduction(): # Get player name and randomly select opponent
            print("Greetings combatant!\n" + "Please enter your name:")
            playerName = input()
            enemyChosen = random.choice(enemyList)
            print ("Nice to meet you, " + playerName + ". I hope you are ready for a fight!\n" + "Today you will be facing...")
            print ("The " + enemyChosen["Name"] + "!")
            print ("Hope you're ready for a tough one!\n")
            return enemyChosen, playerName

    def weaponSelection(): # Get player equipment
            print ("It's time to choose your weapon: ")
            print("\n" .join(d["Name"] for d in weaponList))
            weaponChosen = input()
            while True:
                    if any(d['Name'] == weaponChosen for d in weaponList):
                            return weaponChosen.lower()
                            print("I'm sorry, you can only choose what is available.")
                            weaponChosen = input()

    def weaponReturn(weapon): # to test return variables // probably unnecessary?
            print("Let's see if your " + weapon.lower() + " can defeat your adversary. Good luck!")

    def battleSequence(enemy, player, weapon):
            ambushChance = random.randint(0,1)
            if ambushChance == 0:
                    print("The " + enemy["Name"] + " launched an attack on you from behind!")
                    print (player + " loses 10hp.")
                    print("You get the first attack. You swing your " + weapon + "!")
                    print(player + " does 5 damage to the " + enemy["Name"] + "!")

    playAgain = 'yes'
    while playAgain == 'yes' or 'y':
            enemyName, characterName = introduction()


            battleWeapon = weaponSelection()


            battleSequence(enemyName, characterName, battleWeapon)

1 Answer 1


name your enemies!

Use namedtuple for the enemies/weapons:

Enemy = collections.namedtuple('Enemy', ['name', 'hp', 'attack'])
enemies = [
    Enemy('Orc', 90, 18),

And to access the name, use


Instead of


That way, you don't risk constructing an enemy with a 'name' instead of a 'Name'.

mistaken logic

while playAgain == 'yes' or 'y':

Let's think about what this means. Tip: where are the parenthesis.

while (playAgain == 'yes') or 'y':

So, probably not what you mean. What you mean is

while playAgain in ('yes', 'y'):

string formatting

Probably not your biggest concern, but if you ever want to make translation easy, start now by using Python string formatting.

Instead of

print("The " + enemy["Name"] + " launched an attack on you from behind!")


print("The {} launched an attack on you from behind!".format(enemy["Name"]))


print("The {enemy} launched an attack on you from behind!".format(enemy=enemy["Name"]))

It also reads a lot better!!

  • \$\begingroup\$ Hey thanks for the review! I had no idea about the tuple suggestion and how it works, so I will be incorporating that tonight. Really appreciate your feedback! \$\endgroup\$
    – CampArawak
    Jun 7, 2016 at 1:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.