0

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?

4
  • 1
    What value does set_player_weapon() return? Commented Nov 1, 2017 at 23:04
  • 1
    Do you have any documentation on how the player module works? Commented Nov 1, 2017 at 23:05
  • return set_player_weapon() [Previous line repeated 995 more times] RecursionError: maximum recursion depth exceeded Commented Nov 1, 2017 at 23:06
  • What do you mean, @FabianYing? Commented Nov 1, 2017 at 23:11

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I did this : def set_player_weapon(): import items player_weapon = items.WBrokenSword player_damage = player_weapon.damage return player_weapon, player_damage, that resulted in this: AttributeError: 'tuple' object has no attribute 'player_damage'
Only objects have attributes that can be accessed with dot notation. You access data in tuples either by unpacking them into variables: weapon, damage = set_p_w or with bracket notation: player_damage = set_p_w[0]

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.