I'm getting this error:
[...], line 28, in <module>
PlayerDamage = Dice * int(set_p_w.player_damage)
AttributeError: 'NoneType' object has no attribute 'player_damage'
When I run this code:
import player
Dice = random.randrange(1, 7)
set_p_w = player.set_player_weapon()
PlayerDamage = Dice * set_p_w.player_damage
This is how player.set_player_weapon()
looks like:
def set_player_weapon():
import items
player_weapon = items.WBrokenSword
player_damage = player_weapon.damage
I searched everywhere and tried a bunch of different solutions, but nothing helped me. What is wrong with my code?
From the code you posted, player.set_player_weapon()
doesn’t return anything. So set_p_w
is nothing. You are interacting with set_p_w
as if it is an object, but set_player_weapon()
doesn’t create an object, it just sets two local variables (player_weapon
and player_damage
) and then discards them when the function ends.
The simplest way to get this to work is to have your player.set_player_weapon()
method return a tuple with that information so it can be stored in the a variable outside the function: (player_weapon, player_damage)
.
Tuple Method
def set_player_weapon():
import items
player_weapon = items.WBrokenSword
player_damage = player_weapon.damage
return (player_weapon, player_damage)
player_weapon_damage = player.set_player_weapon()
PlayerDamage = Dice * player_weapon_damage[0]
A better way would be to make an class for Player
which has the attributes player_weapon
and player_damage
as well as methods like def set_player_weapon()
that set and change its attributes.