Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python (retired) Hack-n-Slash The Final Push

Oti Oritsejafor
Oti Oritsejafor
3,281 Points

How do I make weapons do different amount of damage?

I have tried this:

 def attack(self):
    roll = random.randint(1, self.attack_limit)
    weapon_damage = 0
    if self.weapon == 'sword':
      roll += 2
      weapon_damage += 3
    elif self.weapon == 'bow':
      roll += 3 
      weapon_damage += 2
    elif self.weapon == 'axe':
      roll -=1
      weapon_damage += 5
    return roll > 4

in combat.py

and

def player_turn(self):
    choice = input("Do you want to [A]ttack , [R]est, or [Q]uit?".upper())
    if choice == 'A':
      if self.player.attack():
        print("You are attacking the {}".format(self.monster))
        if self.monster.dodge():
          print("The {} dodged!".format(self.monster))
        else:
          if self.player.leveled_up():
            print("Your hit the {} with two hit points on leveling up!".format(self.monster()))
            self.monster.hit_points -= (2 + self.weapon_damage)
          else:
            self.monster.hit_points -= self.weapon_damage

          print("You hit {} with your {}".format(self.monster, self.player.weapon))  
      else:
        print("You missed!")
    elif choice == 'R':
      print("You are resting")          
      self.player.rest() 
    elif choice == 'Q':
      print("You have decided to flee like the coward you are.") 
      sys.exit()
    else:
      self.player_turn()    

in game.py but it just tells me

Traceback (most recent call last):
File "game2.py", line 95, in <module>
Game()
File "game2.py", line 81, in init
self.player_turn()
File "game2.py", line 49, in player_turn
self.monster.hit_points -= self.weapon_damage
AttributeError: 'Game' object has no attribute 'weapon_damage'

I would suggest using a dict to hold the weapon type and the corresponding damage:

weapons = {'sword': 8, 'axe' : 6, 'bow and arrow': 5, 'mace': 4}

2 Answers

Like Jeremy said I once tried with a dictionary where I put different weapons of my choice with different hit points.

For example: weapons = {'sword': 3, 'axe' : 2, 'gun': 7, 'catapult': 2}