python-3.x 能力评分生成器无法按预期工作

dzjeubhm  于 2023-05-19  发布在  Python
关注(0)|答案(1)|浏览(141)

我喜欢这个“D&D随机字符生成器”的大部分在线另一个来源(不记得确切的位置,我会引用它,当我再次找到它),它所有的作品保存它滚动和分配能力得分的地方

#### Ability score generator
def __GetAbilityScores__(characterClass):
    scores = []
    for x in range(6):
        score = []
        for x in range(4):
            score.append(random.randint(1,6))
            score = sorted(score)
            score.pop(0)
            scores.append(sum(score))
    scores = sorted(scores)
    arrangedScores = {"STR":0, "DEX":0, "CON":0, "INT":0, "WIS":0, "CHA":0}
    match characterClass:
        case 'Fighter':
            arrangedScores["STR"] = scores[5]
            arrangedScores["CON"] = scores[4]
            arrangedScores["DEX"] = scores[3]
            arrangedScores["WIS"] = scores[2]
            arrangedScores["CHA"] = scores[1]
            arrangedScores["INT"] = scores[0]
        
        case 'Wizard':
            arrangedScores["INT"] = scores[5]
            arrangedScores["CON"] = scores[4]
            arrangedScores["DEX"] = scores[3]
            arrangedScores["WIS"] = scores[2]
            arrangedScores["CHA"] = scores[1]
            arrangedScores["STR"] = scores[0]

        case 'Rouge':
            arrangedScores["DEX"] = scores[5]
            arrangedScores["CON"] = scores[4]
            arrangedScores["CHA"] = scores[3]
            arrangedScores["STR"] = scores[2]
            arrangedScores["INT"] = scores[1]
            arrangedScores["WIS"] = scores[0]
    return arrangedScores

# chara info is stored here
class character:
    def __init__(self):
        self.race = race[random.randint(0,len(race)-1)]
        self.characterClass = job[random.randint(0,len(job)-1)]
        self.gender = gender[random.randint(0, len(gender)-1)]
        self.name = __GetRandomName__(self.race, self.gender)
        self.abilityScores = __GetAbilityScores__(self.characterClass)

newCharacter = character()

print(vars(newCharacter))

每当我运行这个,其他一切工作,但能力得分回来为“0”全线。任何帮助将是可怕的。我刚刚开始使用python,并将其作为一个项目来帮助我学习更多。

3xiyfsfu

3xiyfsfu1#

我也是龙与地下城的爱好者,希望你的竞选顺利👌
你的错误就在这里。

for x in range(6):
    score = []
    for x in range(4):
        score.append(random.randint(1,6))
        score = sorted(score)
        score.pop(0)
        scores.append(sum(score))

你插入到得分列表中,然后删除列表中唯一的元素,因此,sum总是等于零

我把它从瞄准镜里拿出来帮你修好了。用这个改变那个范围,它应该工作得很好!

scores = []
for x in range(6):
    score = []
    for x in range(4):
        score.append(random.randint(1,6))

    # Sort items from lesser to greater and remove the first item 
    score = sorted(score)
    score.pop(0)

    # Just save the sum of it into score
    scores.append(sum(score))

相关问题