如何链接变量?

时间:2019-03-15 18:28:37

标签: python list drag-and-drop

不确定如何使用此编辑器,我在任何地方都找不到它,所以我想问一下。我正在用python创建DND字符创建器。我列出了各种技能,并希望能够说“如果角色是此类,他们将在此列表中列出对象”。例如。

Appraise, Balance, Bluff, Climb, Concentration, Craft, DecipherScript, Diplomacy,  DisableDevice, Disguise, EscapeArtist, Forgery, GatherInformation, HandleAnimal,  Heal, Hide, Intimidate, Jump, Knowledge, Listen, MoveSilently, OpenLock, Perform,  Profession, Ride, Search, SenseMotive, SleightOfHand, SpeakLanguage, Spellcraft,  Spot, Survival, Swim, Tumble, UseMagicDevice, UseRope = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

ClassSkills=[Appraise, Balance, Bluff, Climb, Concentration, Craft, DecipherScript, Diplomacy, DisableDevice, Disguise, EscapeArtist, Forgery, GatherInformation, HandleAnimal, Heal, Hide, Intimidate, Jump, Knowledge, Listen, MoveSilently, OpenLock, Perform,  Profession, Ride, Search, SenseMotive, SleightOfHand, SpeakLanguage, Spellcraft, Spot, Survival, Swim, Tumble, UseMagicDevice, UseRope]

if class=wizard:
ClassSkills= [Concentration, Craft, DecipherScript, Knowledge, Proffesion, Spellcraft]

if var in ClassSkills:
    SkillPoints= math.floor(Ranks)
else:
    SkillPoints= math.floor(Ranks/2)

通过类向导可以分配其他东西,但是您可能会明白这一点。

我对编程很陌生,这是我的第一个主要程序。我基本上只希望它检测该技能是否为班级技能,然后根据是否是该技能运行另一种算法。

我还将做一些调整以具有不同的技能点变量,以便我可以单独修改列表。

1 个答案:

答案 0 :(得分:2)

我不认为在这里使用if语句是最好的方法。为什么不只将角色类具有的技能定义为类的属性,然后通过属性来总结其技能点呢?我喜欢您正在开发的这个项目,这是进入编程的绝佳方式。

from collections import namedtuple

Skill = namedtuple('Skill', ['name', 'skill_points'])

Appraise = Skill('Appraise', 2)
Balance = Skill('Balance', 3)

class Character():
    def __init__(self, name, skills=None):
        self.name = name
        self.skills = skills

    @property
    def skill_points(self):
        return sum([skill.skill_points for skill in self.skills])

class Wizard(Character):
    def __init__(self, name):
        super().__init__(name, skills=[Appraise, Balance])

bob_the_wizard = Wizard('Bob')
print(bob_the_wizard.skill_points) # 5