【问题标题】:Remove from list as choices are made做出选择时从列表中删除
【发布时间】:2018-03-15 18:40:53
【问题描述】:

我正在尝试创建一个程序,通过随机为其分配种族、类别和统计数据来创建角色。

但是,我希望每个统计数据都具有独特的价值。因此,如果strength 为 8,则其他统计数据都不能为 8。我将如何去做呢?做出选择时是否需要删除列表条目?

我的代码

import random

races = ["Human", "Dwarf", "Elf"]
classes = ["Fighter", "Wizard", "Rogue"]
stats = [8, 10, 11, 12, 14, 15]

Strength = 0
Dexterity = 0
Constution = 0
Intelligence = 0
Wisdom = 0
Charisma = 0

Strength = random.choice(stats)
Dexterity = random.choice(stats)
Constution = random.choice(stats) 
Intelligence = random.choice(stats)
Wisdom = random.choice(stats)
Charisma = random.choice(stats)

race = random.choice(races)
clse = random.choice(classes)

【问题讨论】:

  • 另一种选择是只使用random.shuffle 然后按顺序分配
  • @Hamms 我想说同样的话。但请使用字典和 zip。

标签: python list random choice


【解决方案1】:

创建stats 列表的随机排列,然后按排列顺序分配StrengthDexterity 等。这样做的好处是您无需在创建新角色之前重置列表。

from random import shuffle

# ...
stats = [8, 10, 11, 12, 14, 15]
shuffle(stats)
Strength = stats[0]
Dexterity = stats[1]
Constution = stats[2]
Intelligence = stats[3]
Wisdom = stats[4]
Charisma = stats[5]

附带说明,没有必要将0 的默认值分配给Strength 等,因为它们会在之后立即更改。

【讨论】:

    【解决方案2】:

    正如您在问题中所说,您可以从列表中删除条目:

    Strenght = random.choice(stats)
    del stats[stats.index(Strength)]
    

    就像我上面的答案中所说的那样,一个好的解决方案也是使用random shuffle。加上一个提示,例如Strength = random.choice(stats),您在那一刻声明变量强度并给它一个值,这样您就不必在它上面声明它,这样您就可以删除所有这些Strength = 0 等等。

    【讨论】:

      【解决方案3】:

      您向我们展示的内容适用于 1 个角色。但是,如果我们想创建数百个呢?在 Python 中,一切都是一个类,在这种情况下强烈建议使用它。

      看这个简单的例子:

      import random
      from textwrap import dedent
      
      races = ["Human", "Dwarf", "Elf"]
      classes = ["Fighter", "Wizard", "Rogue"]
      stats = [ 8, 10, 11, 12, 14, 15]
      
      class char:
          def __init__(self, races, classes, stats):
              random.shuffle(stats) # <--- this solves your problem
              self.race = random.choice(races)
              self.cls = random.choice(classes)
              self.stats = dict(zip(['Strength','Dexterity','Constution',
                                     'Intelligence','Wisdom','Charisma'],stats))
      
          def __str__(self):
              s = dedent('''\
              Your character is a....
              Race: {}
              Class: {}
              Stats: {}''').format(self.race, self.cls, self.stats)
              return s
      
      char1 = char(races,classes,stats) # Creates char1 based on char class
      char2 = char(races,classes,stats) # Creates char2 ...
      
      print(char1) # printing a class will call the __str__ function if it exists
      print()
      print(char2)
      

      一个类可以保存变量和其他函数,在这种情况下,我们创建了三个变量(race、cls 和 stats),并添加了一个打印函数来轻松打印我们想要的内容。

      当我运行它时返回这个:

      Your character is a....
      Race: Elf
      Class: Rogue
      Stats: {'Dexterity': 11, 'Charisma': 15, 'Constution': 12, 'Wisdom': 10, 'Intelligence': 14, 'Strength': 8}
      
      Your character is a....
      Race: Dwarf
      Class: Fighter
      Stats: {'Dexterity': 14, 'Charisma': 8, 'Constution': 12, 'Wisdom': 11, 'Intelligence': 15, 'Strength': 10}
      

      【讨论】:

        【解决方案4】:

        您可以将 random.choice 的值存储在临时文件中。变量和使用

        stats.remove(temp)
        

        【讨论】:

          猜你喜欢
          • 2019-05-05
          • 2013-11-15
          • 2020-03-29
          • 2013-06-23
          • 2013-07-13
          • 1970-01-01
          • 2015-09-10
          • 2021-05-03
          • 2016-11-01
          相关资源
          最近更新 更多