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

时间:2018-12-05 23:16:07

标签: python dictionary

玩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进行练习,所以不会出现逻辑错误。

谢谢

1 个答案:

答案 0 :(得分:1)

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

<form enctype="multipart/form-data" action="add.php" method="POST">

 Stocknr: <input type="text" name="stocknr">
 <p style="font-family:krona-one,sans-serif;colour:#ffffff;" 
 size="18px">**Alphanumerical, No Spacing, Max 25 Characters**</p>
      <br><br>
 Year: &nbsp;&nbsp;&nbsp;<input type="text" name="year">
 <p style="font-family:krona-one,sans-serif;colour:#ffffff;" 
 size="18px">**Numbers Only, Max 5 Character**</p>
 <br><br>

  Brand: &nbsp;&nbsp;<input type="text" name="brand">
  <p style="font-family:krona-one,sans-serif;colour:#ffffff;" 
  size="18px">**Max 20 Characters**</p>
  <br><br>
  Model: &nbsp;&nbsp;<input type="text" name="model">
  <p style="font-family:krona-one,sans-serif;colour:#ffffff;" 
  size="18px">**Max 20 Characters**</p>         
  <br><br>

  <?php
 $servername = "servername";
 $dbusername = "username";
 $dbpassword = "password";
 $dbname = "dbname";

 $con=mysqli_init();
 if (!$con)
 {
 die("mysqli_init failed");
 }
 if (!mysqli_real_connect($con,$servername,$dbusername,$dbpassword,$dbname))
 {
 die("Connect Error: " . mysqli_connect_error());
  }

 echo "<font face='krona-one' size='3'>Vehicle Type:";
 echo "<select name='class' 'class' class='class'><option value='all'><font 
 face='krona-one' size='3'>Select</option>";



  mysqli_select_db($con,"$dbname");

 $sql = "SELECT type FROM class";
 $result = mysqli_query($con,$sql);

 while ($row = mysqli_fetch_array($result)) {
 echo "<option value='" . $row['type'] ."'>" . $row['type'] ."</option>";
 }
 echo "</select>";
  ?> 
 <br><br>
 Mileage: <input type="text" name = "mileage">
 <p style="font-family:krona-one,sans-serif;colour:#ffffff;" 
 size="18px">**Numbers, No Spacing, Max 25 Characters**</p>          
 <br><br>
 Colour: &nbsp;&nbsp;&nbsp;<input type="text" name = "colour">
 <p style="font-family:krona-one,sans-serif;colour:#ffffff;" 
 size="18px">**Max 20 Characters, No Spacing**</p>
 <br><br>
 Transmission: &nbsp;&nbsp;&nbsp;<input type="text" name = "trans">
 <p style="font-family:krona-one,sans-serif;colour:#ffffff;" 
 size="18px">**Max 20 Characters, No Spacing**</p>
<br><br>
Fuel: &nbsp;&nbsp;&nbsp;<input type="text" name = "fuel">
<p style="font-family:krona-one,sans-serif;colour:#ffffff;" 
size="18px">**Max 20 Characters**</p>
<br><br>
Description: <br><textarea name="description" rows="4" cols="40"> 
</textarea><br><br>
Price: &nbsp;&nbsp;&nbsp;<input type="text" name = "price">
<p style="font-family:krona-one,sans-serif;colour:#ffffff;" 
size="18px">**Numbers Only, No Spacing, Leave out "R"**</p>
<br><br>
Photo: &nbsp;&nbsp;<input type="file" name="photo"><br>
Photo2: <input type="file" name="photo2"><br>
Photo3: <input type="file" name="photo3"><br>
Photo4: <input type="file" name="photo4"><br>
Photo5: <input type="file" name="photo5"><br>
Photo6: <input type="file" name="photo6"><br>
Photo7: <input type="file" name="photo7"><br>
Photo8: <input type="file" name="photo8"><br>
Photo9: <input type="file" name="photo9"><br>
Photo10: <input type="file" name="photo10"><br>
<br>
<input type="submit" value="Add" >
</form>

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

已更新

以下是一些代码:

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

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