'Subtracting objects
I am a beginner and I was trying to subtract the attack of the health from the dark knight from the attack of a player but when I do a print of the health after the operation it stays the same.
the class of the player looks like this.
class Player:
def __init__(self, name, health, attack, weapon):
self.name = name
self.health = health
self.attack = attack
self.weapon = weapon
def return_name(self):
return self.name
def return_health(self):
return self.health
def return_attack(self):
return self.attack
def return_weapon_damage(self):
return self.weapon.return_modifier
def damage_plr(self, damage):
self.health -= damage
def attack_enny(self, target):
target -= self.return_attack() + self.weapon.return_modifier()
and this is where I wrote the subtraction.
from player import Player
from Weapon import Weapon
import random
from Enn import Enemy
##########-Weapon-##########
knife = Weapon('Knife', 5)
blade = Weapon('Blade', 8)
shield = Weapon('Shield', 7)
rand_weapon = [knife, blade, shield]
##########-Weapon-##########
##########-Enemy-##########
drk_att = random.randint(4, 7)
drk_kng = Enemy('Dark Knight', 45, drk_att)
opp_choose = []
##########-Enemy-##########
choose_name = input("Choose your name: ")
random.shuffle(rand_weapon)
player1 = Player(choose_name, 50, 2, rand_weapon[0])
print()
print('Welcome', choose_name, '!!!\n')
print("You've come across the path of a Dark Knight. He don't seems to happy to see you.\n"
"You know that you will need to prepare for combat. Your weapon in this fight will be...")
print('A', player1.weapon.return_name(), 'that does', player1.weapon.return_modifier(), 'damage!\n')
print('##########################################################################################\n')
while drk_kng.return_health() >= 0 or player1.return_health() >= 0:
choice = input('What would you like to do.(Attack or protect): ')
if choice == 'attack' or choice == 'Attack':
player1.attack_enny(drk_kng.return_health())
print('You have delt', player1.weapon.return_modifier(), 'damage and he know have', drk_kng.return_health(), '\n')
Solution 1:[1]
I'm going to assume that your Enemy class is similar to you Player class, in which case then I will further assume that drk_kng.return_health() return a number, in which case the problem is simple that you don't store the result anywhere.
You might think you do on attack_enny on the Player class when you do target -= ... as that is the job of the a-=b but that is only true for mutable object, number and string are immutable objects, any attend to modify them result in a new instance of that immutable object being created and the a-=b become a=a-b which is just a reassignment, for the -= (or any of its variations) to work as you expect you need to work with object that are mutable (and of course that they have said operator defined for them)
Now lets look at a quick example
>>> def add(a,b):
a+=b
print(a)
>>> n=9
>>> add(n,1)
10
>>> n
9
>>> x=[]
>>> add(x,[42])
[42]
>>> x
[42]
>>>
the sample with n is what you're doing, n by being a number is immutable an and thus inside the function add the += fallback to a reassignment so the a inside the function is a new variable with the new result and is no longer the same n from before, and thus the n outside remain unchanged, but when you apply the same function to a mutable object like the list x you can see the change both inside the function and outside too.
So and easy fix for problem is to pass your Enemy class instance into your method like for example player1.attack_enny(drk_kng) and change attack_enny accordingly
def attack_enny(self, enemy):
enemy.health -= self.return_attack() + self.weapon.return_modifier()
Another way is, given that value you subtract depend on two other you player class can have a new method that return that value
def total_attack(self):
return self.return_attack() + self.weapon.return_modifier()
and them in your Enemy class include a new method for taking damage, like for example
def take_dmg(self, damage):
self.health -= damage
and then your player1.attack_enny(drk_kng.return_health()) become drk_kng.take_dmg(player1.total_attack())
Apart from that, all your getter functions (all your return_*) are unnesesary, you can change your Player class to just
class Player:
def __init__(self, name, health, attack, weapon):
self.name = name
self.health = health
self.attack = attack
self.weapon = weapon
def damage_plr(self, damage):
self.health -= damage
def total_attack(self):
return self.attack + self.weapon.return_modifier()
and then change all your player1.return_X() to just player1.X, in python you don't really need to define getter or setter unless said method do something else beside just get or set a value
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Copperfield |
