繁体   English   中英

根据字典中的键检查随机生成列表的值并返回字典值

[英]Check Value of Randomly Generated List against Keys in Dictionary and return Dictionaries Value

使用 Python 并测试我的技能,我想构建一个 Python 脚本,该脚本以 D&D 5E 中创建角色的规则为中心。 到目前为止的脚本:


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

我遇到的问题是让脚本查看技能生成的值并根据我的字典检查它,然后返回与技能返回的数字匹配的相应键的值。

这是一个片段,来源在这里5E Character creator - python edition

我已经在这里呆了大约一天半了,但我却是一片空白。 当然,我在工作时使用 pythonfiddle 进行练习,所以我没有遇到逻辑错误。

谢谢

好的,所以我得到的是:您正在生成一个随机强度分数。 您想将它与说明静态分数和修饰符之间关系的字典进行比较。 应该足够简单了。 尝试这个:

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

这是有效的,因为我们所说的是最初,我们遍历修饰符字典中的每个键(左侧)。 如果当前键的右侧等于等于实力得分关键的右侧,打印真。 从功能上讲,我不确定为什么要测试布尔相等性,但无论哪种方式都应该这样做。 让我知道它是否有效!

更新

这是一些代码:

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]))

输出:

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

只是玩得开心

也许这会在以后派上用场!

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]]))

输出:

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

已编辑

事实证明它比我们想象的还要简单!

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

因此,在执行 If 语句时,您甚至不需要使用字典索引进行比较。 您可以使用迭代器中的“key”并将其与能力得分进行比较。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM