I'm using python for creating a Dungeons and Dragons Character Creator, and am currently programming the weapons. I've recreated some code that has failed to work in previous similar environments, and I was wondering about a way for it to work without needing an if statement for each individual variable, so that editing for additional properties and items might be available.
The goal is to create an Weapon(Object) that has only the Properties(variables) associated with it.
Current Code:
class Weapon:
def __init__(self, finesse, light, thrown, two_handed, versatile):
property_list = [finesse, light, thrown, two_handed, versatile]
for prop in property_list:
if prop != 0:
self.prop = prop
dagger = Weapon('finesse', 'light', 'thrown', 0, 0)
Currently the variable wouldn't be created for the weapon; thus
dagger.finesse
Does not exist. Ideally 'finesse' would be stored in the variable dagger.finesse, while the variable dagger.two_handed would not be created by the code.
I'm probably overlooking something simple, and there might be duplicate questions, but I can't find a similar question with the answer I'm looking for.
Thank you to all who reply
Edits
Thank you to @WKPlus for some formatting assistance; I'm new to the stackoverflow website
I've realized thanks to @abarnert that the better way to do this is to have the porperties listed in a variable and to check that variable later in the code for the property, like so
class Weapon:
def __init__(self, finesse, light, thrown, two_handed, versatile):
property_list = [finesse, light, thrown, two_handed, versatile]
self.properties = []
for prop in property_list:
if prop != 0:
self.properties.append(prop)
However, I'm still interested in seeing how my original question might be answered, as it could still be useful to run something like my original without the exclusion statement.
Weapon.finesseto be the string'finesse'or nonexistent? Wouldn't it be better to have it be, say, a bool that's true for daggers and false for clubs?try: player.weapon.finesse except AttributeError:all over the code?Weapon.dagger.propertiesand check that for the values as needed.if 'thrown' in weapon.properties:), or print out the whole list if needed.