Proper way to chain functions?

# Function which takes the party size, average party level, and desired difficulty to return the correct XP budget
# within the encounter_table dictionary
def xp_budget(party_size, party_level, difficulty):
if party_level > 20: # currently no data for levels > 20
return 'Party level should not exceed 20, please enter an average party level between 1 and 20.'
elif party_level <= 0 or party_size <= 0: # need sanity check that input is > 0
return 'Party level should be between 1 and 20, while party size should be 1 or greater.'
else:
return (encounter_table[difficulty.lower()][party_level - 1]) * party_size

should be

def xp_budget(party_size,partly_level,difficulty):
    '''Function which takes the party size, average 
    party level, and desired difficulty to return 
    the correct XP budget within the encounter_table 
    dictionary'''
    if party_level > 20 or party_level <1 or party_size<1:
        raise AssertionError('Party level should be between 1 and 20, while party size should be 1 or greater.')
    return (encounter_table[difficulty.lower()][party_level - 1]) * party_size

Because you want a return value of a predictable type. Also, the docstring. Also see PEP 8.

But really, you should ensure party size and level are accurate before calling this function. And make sure difficulty is already lower, rather than converting it in every function.

I won't say much about the globals. But, if you want your code to be more flexible and less awkward, pass in any variables that will be used in the function. Like, when you have an encounter, you'd probably want the enemy to be biome or environment constrained, so the function that determines the encounter would take an encounterdict that is not your global encounterdict.

/r/learnpython Thread