1

Playing with Python, and to test my skills, I wanted to build a Python Script that is centered on the rules for creating a character in D&D 5E. Script so far:


for x in range (1):
    strength.append(random.randint(3,18))
str_score = strength


Ability_Score_Modifiers = {'1' : '-5',
                           '2' : '-4',
                           '3' : '-4',
                           '4' : '-3',
                           '5' : '-3',
                           '6' : '-2',
                           '7' : '-2',
                           '8' : '-1',
                           '9' : '-1',
                           '10' : '0',
                           '11' : '0',
                           '12' : '+1',
                           '13' : '+1',
                           '14' : '+2',
                           '15' : '+2',
                           '16' : '+3',
                           '17' : '+3',
                           '18' : '+4',
                           '19' : '+4',
                           '20' : '+5',
                           '21' : '+5',
                           '22' : '+6',
                           '23' : '+6',
                           '24' : '+7',
                           '25' : '+7',
                           '26' : '+8',
                           '27' : '+8',
                           '28' : '+9',
                           '29' : '+9',
                           '30' : '+10'}

for keys in Ability_Score_Modifiers.keys() and str_score:
        if Ability_Score_Modifiers.keys([str_score]) == Ability_Score_Modifiers.keys():
            print ('True', value)
        else:
            pass

The problem I am having is getting the Script to look at the value generated by the skill and check it against my dictionary and then return the value for the respective key that matches the number returned for the skill.

This is a snippet the source is here 5E Character creator - python edition

I've been at this for about a day and a half and I'm coming up blank. Of course, I'm using pythonfiddle to practice while at work so I am not getting my logic errors.

Thanks

Eb946207
  • 748
  • 8
  • 26
3d6-dm
  • 13
  • 2
  • Probably shouldn't have the #d tag on this question? It's about Python and not about the D programming language. – dhasenan Dec 05 '18 at 23:39

1 Answers1

1

Okay, so what I'm getting is: You're generating a random strength score. You want to compare it to the dictionary which dictates the relationship between the static score and the modifier. Should be simple enough. Try this:

for key in Ability_Score_Modifiers:
        if Ability_Score_Modifiers[key] == Ability_Score_Modifiers[int(str_score)]:
            print('True', value)

This works because what we're saying is that initially, we're looping through every key (left side) in the modifier dictionary. If the right side of the current key is equal to the right side of the key which is equal strength score, print True. Functionally, I'm not sure why you want to test the boolean equality but either way this should do. Let me know if it works!

Updated

Here's some code:

import random
str_score = str(random.randint(3,18))

Ability_Score_Modifiers = {'1' : '-5', '2' : '-4', '3' : '-4', '4' : '-3', '5' : '-3', '6' : '-2', '7' : '-2', '8' : '-1', '9' : '-1', '10' : '0', '11' : '0', '12' : '+1', '13' : '+1', '14' : '+2', '15' : '+2', '16' : '+3', '17' : '+3', '18' : '+4', '19' : '+4', '20' : '+5', '21' : '+5', '22' : '+6', '23' : '+6', '24' : '+7', '25' : '+7', '26' : '+8', '27' : '+8', '28' : '+9', '29' : '+9', '30' : '+10'}
for key in Ability_Score_Modifiers:
        if Ability_Score_Modifiers[key] == Ability_Score_Modifiers[str_score]:
            print("True")
            print("Stat score of {} grants a modifier of {}".format(str_score, Ability_Score_Modifiers[str_score]))

Output:

True
Stat score of 15 grants a modifier of +2
True
Stat score of 15 grants a modifier of +2

Just Having Fun

Maybe this will come in handy later!

import random
#Generate our Str, Dex, Con, Int, Wis, Cha
Ability_Scores = {}
for n in ['Str', 'Dex', 'Con', 'Int', 'Wis', 'Cha']:
    Ability_Scores[n] = str(random.randint(3,18))

Ability_Score_Modifiers = {'1' : '-5', '2' : '-4', '3' : '-4', '4' : '-3', '5' : '-3', '6' : '-2', '7' : '-2', '8' : '-1', '9' : '-1', '10' : '0', '11' : '0', '12' : '+1', '13' : '+1', '14' : '+2', '15' : '+2', '16' : '+3', '17' : '+3', '18' : '+4', '19' : '+4', '20' : '+5', '21' : '+5', '22' : '+6', '23' : '+6', '24' : '+7', '25' : '+7', '26' : '+8', '27' : '+8', '28' : '+9', '29' : '+9', '30' : '+10'}
for score in Ability_Scores:
            print("{} score of {} grants a modifier of {}".format(score, Ability_Scores[score], Ability_Score_Modifiers[Ability_Scores[score]]))

Output:

Str score of 7 grants a modifier of -2
Dex score of 12 grants a modifier of +1
Con score of 17 grants a modifier of +3
Int score of 8 grants a modifier of -1
Wis score of 12 grants a modifier of +1
Cha score of 5 grants a modifier of -3

Edited

Turns out its even simpler than we thought!

import random
str_score = str(random.randint(3,18))

Ability_Score_Modifiers = {'1' : '-5', '2' : '-4', '3' : '-4', '4' : '-3', '5' : '-3', '6' : '-2', '7' : '-2', '8' : '-1', '9' : '-1', '10' : '0', '11' : '0', '12' : '+1', '13' : '+1', '14' : '+2', '15' : '+2', '16' : '+3', '17' : '+3', '18' : '+4', '19' : '+4', '20' : '+5', '21' : '+5', '22' : '+6', '23' : '+6', '24' : '+7', '25' : '+7', '26' : '+8', '27' : '+8', '28' : '+9', '29' : '+9', '30' : '+10'}
for key in Ability_Score_Modifiers:
        if key == str_score:
            print("True")
            print("Stat score of {} grants a modifier of {}".format(str_score, Ability_Score_Modifiers[str_score]))
True
Stat score of 3 grants a modifier of -4

So when doing the If statement, you don't even need to compare using the dictionary indexing. You can just use "key" from the iterator and compare it to the ability score.

KuboMD
  • 684
  • 5
  • 16
  • The `.keys()` is not needed, looping through a `dict` will loop through the keys on its own, so do this `for key in Ability_Score_Modifiers: #etc...` – Eb946207 Dec 05 '18 at 23:24
  • 1
    No dice thus far, but it very well could be a problem with running this on pythonfiddle.com. Might be a few days before I can test this in IDLE proper. – 3d6-dm Dec 05 '18 at 23:27
  • Give me a moment, I'll test on my machine and report back. – KuboMD Dec 05 '18 at 23:28
  • @ Ethan - It's a Debug test, if it can find it I want a visual. I will append the loop to print "Modifier : (value)" once I know the code works. – 3d6-dm Dec 05 '18 at 23:32
  • @3d6-dm I am not sure that I understand. My changes make the code work **the same**. – Eb946207 Dec 05 '18 at 23:33
  • @3d6-dm Take a look :) – KuboMD Dec 05 '18 at 23:47
  • @EthanK I think pythonfiddle.com doesn't like this script confined inside a def. I was able to return the same variables if this code is alone, but not if I add it into a def. Thank you for pointing me in the right direction though. – 3d6-dm Dec 06 '18 at 00:06
  • @3d6-dm No, `def` statements work differently because of [python scopes](https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules). – Eb946207 Dec 06 '18 at 00:18
  • Update: I switch to prel.it and got it to work. Thank you again. – 3d6-dm Dec 06 '18 at 00:26
  • @KuboMD - Excellent addition, but I have additional variables I have to account for like Race, which adjusts the Strength Value, and then the Modifier – 3d6-dm Dec 06 '18 at 00:27
  • Hey KuboMD, in your code, why do the outputs print twice? – 3d6-dm Dec 06 '18 at 00:48