Python Text-based Game Equip Armor Stats

Question:

I’m working on a Python text-based rpg and ran into a problem I can’t figure out how to fix. I’m trying to get a system that is constantly updating to tell the player what his/her stats should be depending on the armor or weapon equipped.
here is an sample of the class from my code:

import sys
import random

class Player:
    def __init__(self, name):
        self.name = name
        self.health = 100
        self.maxhealth = 100
        self.weap = "Rusty Sword"
        self.armH = "None"
        self.armB = "Wor Shirt"
        self.armA = "None"
        self.armL = "Worn Pants"
        self.armF = "Worn Boots"
        self.gold = 100
        self.stre = 1
        self.magi = 0
        self.rang = 0
        self.defe = 0
        self.level = 1
        self.exp = 0
        self.maxexp = 100
        self.pots = 0
        self.arrows = 0
        self.inventory = ["Rusty Sword", 
                          "Worn Shirt", 
                          "Worn Pants", 
                          "Worn Boots"]
        self.spells = []
## Armor and Weapon Stats #####
    ## WEAPON BUFFS ##
        if self.weap == "Rusty Sword":
            self.stre += 10 
        if self.weap != "Rusy Sword":
             self.stre -= 10

The bottom code shows if you have a certain item equip
it will change your stats but the problem I have currently is when
you unequip armor or weapons it doesn’t decrease or even change your stats
like it is supposed to. Basically I’m just figuring out a way to create an updating system that updates the player’s stats accordingly depending on what items the have equipped.

Asked By: King Arthur

||

Answers:

It sounds like each character has a base attribute score, but the effective score is the base modified by various pieces of equipment. This is a good use case for a property.

class Player:
    def __init__(self, name):
        # ...
        self.base_stre = 1
        # ...

    @property
    def stre(self):
        stre = self.base_stre
        if self.weap == "Rusty Sword":
            stre += 10
        # etc
        return str
Answered By: chepner
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.