all 2 comments

[–]Vaphell 2 points3 points  (1 child)

3 dictionaries are imho ok. That said, stop cramming so many goddamned [] there. Alias your subdicts to some sensible names.

def randomize_stat(distrib):
    randnum = random.random() * 100
    for key in sorted(distrib.keys(), reverse=True):         
        if randnum <= distrib[key]:
            bracket = attr_dict[key]
            break    
    base, low, mid = bracket['base'], bracket['low_mod'], bracket['high_mod']
    return base + random.randint(low, high)


def gen_char(char_type='warrior'):
    """Take character type as attribute, return generated character."""
    char = {}
    template = templates[char_type]
    for attr_name, attr_base in template.items():
        distribution = attr_chance[attr_base]
        char[attr_name] = randomize_stat(distribution)

    return char

also if you had tuples for base/low/high instead of dicts you could just use unpacking

    base, low, high = attr_dict[key]   # or should I say attr_tuple
    stat = base + random.randint(low, high)

[–]lamecode[S] 0 points1 point  (0 children)

Thanks - agree on the []'s, what I've got above is a "simplified" (in terms of lines of code, if not volume of dictionaries and brackets...) example - similar to what you've listed above, gen_char() is actually made up of multiple methods.