Python: Sort, randomize specific attributes, then manipulate

Question:

I need to find the lowest attribute value, add 1, as well as randomly manipulate a random attribute value for a given set of class instance attributes. My ‘Player’ class has many attributes,

    self.name = name
    self.level = 1
    self.experience = 0
    self.gold = 500000
    self.wielded_weapon = short_sword
    self.armor = padded_armor
    self.shield = no_shield
    self.boots = leather_boots

but I am concerned with these 6:

        self.strength = 15  
        self.dexterity = 14  
        self.constitution = 13  
        self.intelligence = 12 
        self.wisdom = 8  
        self.charisma = 10

I originally tried sorting a list, and then moved to a dictionary and tried..

ability_dict = {1: self.strength, 2: self.dexterity, 3: self.constitution, 4: self.intelligence, 5: self.wisdom, 6: self.charisma}

I tried:

        min_value = min(ability_dict.values())
        min_value += 1

and

rndm_attribute = random.choice(list(ability_dict.values()))
rndm_attribute +=1

And also:

        minimum_key = min(ability_dict, key=ability_dict.get)
        ability_dict[minimum_key] += 1

I learned that I am not affecting the actual attributes of the object, but merely values in the list or dictionary.
How can I sort, randomize and then manipulate the attributes themselves?

Asked By: misfit138

||

Answers:

You can use the __dict__ attribute of your class. By editing player.__dict__ directly, or a variable which you derived from it (player_dict in the code below), you can edit your object’s attributes.

import random

class Player:
    def __init__(self):
        self.strength = 15
        self.dexterity = 14
        self.constitution = 13
        self.intelligence = 12
        self.wisdom = 8
        self.charisma = 10

player = Player()

print(player.__dict__)
player_dict = player.__dict__

# Define list of attributes you are allowed to change
attributes = ["strength", "dexterity", "constitution", "intelligence", "wisdom", "charisma"]
player_dict_subset = {k: v for k, v in player_dict.items() if k in attributes}

# Find the minimum attribute name
min_attribute = min(player_dict_subset, key=player_dict_subset.get)
print("Minimum attribute:", min_attribute)

# Add one to min attribute
player_dict[min_attribute] += 1
print(player.__dict__)

# Choose random attribute name
random_attribute = random.choice(list(player_dict_subset.keys()))
print("Random attribute:", random_attribute)
player_dict[random_attribute] += 1

print(player.__dict__)

Output:

{'strength': 15, 'dexterity': 14, 'constitution': 13, 'intelligence': 12, 'wisdom': 8, 'charisma': 10}
Minimum attribute: wisdom
{'strength': 15, 'dexterity': 14, 'constitution': 13, 'intelligence': 12, 'wisdom': 9, 'charisma': 10}
Random attribute: charisma
{'strength': 15, 'dexterity': 14, 'constitution': 13, 'intelligence': 12, 'wisdom': 9, 'charisma': 11}

Note: Editing player_dict_subset will not change the object’s attributes because it was made from a dict comprehension. You need to edit player.__dict__ or player_dict instead.

Answered By: Angus Nicolson
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.