Issue
I'm currently working with the following code:
def ability_stat_roll():
stat_rolls_stored = []
for i in range(6):
stat_rolls = []
for j in range(4):
num = random.randint(1, 6)
stat_rolls.append(num)
del stat_rolls[stat_rolls.index(min(stat_rolls))]
stat_rolls_stored.append(sum(stat_rolls))
stat_rolls_stored.sort(reverse=True)
print(stat_rolls_stored)
ability_stat_roll()
Which would result in a list like below: (For example)
stat_array = [15, 14, 13, 12, 10, 8]
What I'm trying to figure out is is there a way to take the values from the list and input into a dict for a more user friendly way, example of dict below:
stat_dict = {"str": 0, "dex": 0, "con": 0, "int": 0, "wis": 0, "cha": 0}
I have attempted to try to do the Update Dict
function but I keep getting errors that it's not valid form of Update Dict
. I don't want to append the list as the dict value above already has keys that I would want. Is there a function that would be able to update the dict values from a list?
What I want the end result to be is the following:
"str": 15, "dex": 14, "con": 13, "int": 12, "wis": 10, "cha": 8
Is this possible to do? I'm new to Python and just trying to get my hands wet with trying to create different types of programs.
Answer thanks to the comment below:
def ability_stat_roll():
stat_rolls_stored = []
for i in range(6):
stat_rolls = []
for j in range(4):
num = random.randint(1, 6)
stat_rolls.append(num)
del stat_rolls[stat_rolls.index(min(stat_rolls))]
stat_rolls_stored.append(sum(stat_rolls))
stat_rolls_stored.sort(reverse=True)
print(stat_rolls_stored)
null_stat_dict = {"str": 0, "dex": 0, "con": 0, "int": 0, "wis": 0, "cha": 0}
for i, stat in zip(null_stat_dict, stat_rolls_stored):
null_stat_dict[i] += stat
print(null_stat_dict)
ability_stat_roll()
Which when ran the below is the result:
[17, 13, 12, 11, 10, 4]
{'str': 17, 'dex': 13, 'con': 12, 'int': 11, 'wis': 10, 'cha': 4}
Solution
You can zip those two and add the number in stat_array
to its corresponding key in stat_dict
:
for k, num in zip(stat_dict, stat_array):
stat_dict[k] += num
Output:
{'str': 15, 'dex': 14, 'con': 13, 'int': 12, 'wis': 10, 'cha': 8}
Answered By - enke
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.