Question: I want to make a randomizer for the SPECIAL stats from Fallout New Vegas, i've built most of the code, but there's cases that the sum of the variables exceed/are below the cap of 40.

Is there a way to limit them, or in cases that the sum is below or over 40, distribute the difference?

```
strength = random.randint(1, 10)
perception = random.randint(1, 10)
endurance = random.randint(1, 10)
charisma = random.randint(1, 10)
intelligence = random.randint(1, 10)
agility = random.randint(1, 10)
luck = random.randint(1, 10)
sum = strength + perception + endurance + charisma + intelligence + agility + luck

diff = 40 - sum
if diff < 0:
    diff = (diff * - 1)

print("=======================")
print("Strength:", strength)
print("Perception:", perception)
print("Endurance:", endurance)
print("Charisma:", charisma)
print("Intelligence:", intelligence)
print("Agility:", agility)
print("Luck:", luck)
print("Total:", sum)
print("Difference:", diff)
print("=======================")

```

Answer: 
I would not recommend generating the attributes independently from each other (Green Cloak Guy has provided an instructive answer on how this can be done better).

That said, if for some reason you want to do it nonetheless, you can distribute the differences across the attributes as follows:

```
import random

strength = random.randint(1, 10)
perception = random.randint(1, 10)
endurance = random.randint(1, 10)
charisma = random.randint(1, 10)
intelligence = random.randint(1, 10)
agility = random.randint(1, 10)
luck = random.randint(1, 10)

list_attributes = [strength, perception, endurance, charisma, intelligence, agility, luck]
sum_attributes = sum(list_attributes)
diff = 40 - sum_attributes

if diff != 0:
    diff_partial = diff/len(list_attributes)
    for i, attribute in enumerate(list_attributes):
        list_attributes[i] = attribute + diff_partial

strength, perception, endurance, charisma, intelligence, agility, luck = list_attributes

```

Keep in mind that, since `random` returns `float` values, your updated attributes will be `float`s as well. In case you need them as `int`, you could, for example, use `int()` inside the `for` loop.
I would just generate the seven stats, and depending on the sum total, add or subtract randomly until the sum equals 40...

```
import random
stats = []

## Generate the stats
for x in range(7):
    stats.append(random.randint(1,10))

## If the sum total is 40, leave em.
if sum(stats) == 40:
    pass

## If it is less than 40, add +1's randomly until you reach 40.
elif sum(stats) < 40:
    while sum(stats) != 40:
        stat = random.randint(0,6)
        if stats[stat] != 10:
            stats[stat] = stats[stat] + 1

## If it is more than 40, subtract randomly until you reach 40.
elif sum(stats) > 40:
    while sum(stats) != 40:
        stat = random.randint(0,6)
        if stats[stat] != 1:
            stats[stat] = stats[stat] - 1

strength = stats[0]
perception = stats[1]
endurance = stats[2]
charisma = stats[3]
intelligence = stats[4]
agility = stats[5]
luck = stats[6]

```