Python Analyzer, D&D Character

The tests ask you to write a Character class with a single ability method. The analyzer (PyLint) complains [*R0903 too-few-public-methods*] was reported by Pylint.

Thoughts on turning off R0903 for that exercise?

1 Like

Not sure I want to do that right now.

The instructions don’t say that there needs to be only one method, they ask the student to “Write a random character generator that follows the above rules.”.

If you write a dice_roll method (static or not), and have ability return a random ability value, the tests pass and the linter doesn’t complain.

Yes - the tests imply one method … but they don’t require one method.

There are quite a few solutions that separate out ability from dice_roll – weather it’s a static method, or an instance method.

1 Like

TIL that making the solution a dataclass also satisfies the linter. It’s sorta awkward (may as well write an __init__, if you have to write a __post_init__), but it does work.:

from random import choice, sample
from dataclasses import dataclass

@dataclass
class Character:
    
    strength: int = 0
    dexterity: int = 0
    constitution: int = 0
    intelligence: int = 0
    wisdom: int = 0
    charisma: int = 0
    hitpoints: int = 0
    
    def __post_init__(self):
        for ability in vars(self):
            setattr(self, ability, dice_rolls())
        
        self.hitpoints = 10 + modifier(self.constitution)

    def ability(self):
        return choice([*vars(self).values()])
          
  
def dice_rolls():
    values = sample(range(1, 7), 4)
    return sum(values) - min(values)
        
def modifier(constitution):
    return (constitution - 10)//2