2013-07-29 47 views
0

我正在阅读“Python编程的绝对开始第3版”。其中一个挑战是:字典对变量

编写角色扮演游戏的角色创建者程序。玩家应该获得30点的技能属性:力量,健康,智慧和敏捷。该 球员应该能够从池花点上的任何 属性,也应该能够从 属性取分,并把它们放回池中。

起初我与变量

pool = 30 
strength = 0 
health = 0 
wisdom = 0 
dexterity = 0 

这部分是关于列表和字典写的。所以我的问题是:以这种方式使用变量还是可以使用字典更好?如果是这样,它是否更有效率?即:

attributes = { 
      "strength" : 0, 
      "health" : 0, 
      "wisdom" : 0, 
      "dexterity" : 0 
      } 
+0

我宁愿第二个,因为那么你可以循环通过 – Doorknob

+1

为了更灵活的方法:属性的集合看起来非常适合类,比字典或一堆变量更适合 –

+0

我还没有得到在这本书中尚未有类,但是我已经看到了它们的一些例子。所以如果我要做一个'def attributes:'类并且在那里分配所有的强度,健康等值。我可以稍后打电话给他们,或者用attributes.strength,attributes.health等来改变他们。 – mccdlibby

回答

3

简而言之:我会去找字典。

为了让它长久:这可能是一个很好的例子,直接钻研面向对象编程。

#! /usr/bin/python3 

class Character: 
     class AbilityScoreOutOfBoundsException (Exception): pass 

     def __init__ (self, name): 
      self.name = name 
      self.stats = {k: 1 for k in ['STR', 'DEX', 'WIS', 'INT'] } 

     @property 
     def strength (self): return self.stats ['STR'] 

     @property 
     def dexterity (self): return self.stats ['DEX'] 

     @property 
     def wisdom (self): return self.stats ['WIS'] 

     @property 
     def intelligence (self): return self.stats ['INT'] 

     @strength.setter 
     def strength (self, amount): self.setStat ('STR', amount) 

     @wisdom.setter 
     def wisdom (self, amount): self.setStat ('WIS', amount) 

     @dexterity.setter 
     def dexterity (self, amount): self.setStat ('DEX', amount) 

     @intelligence.setter 
     def intelligence (self, amount): self.setStat ('INT', amount) 

     def setStat (self, which, amount): 
      if amount < 1: raise Character.AbilityScoreOutOfBoundsException ('Beware hero! Thou wert about to smite thyself.') 
      if self.total + amount - self.stats [which] > 30: raise Character.AbilityScoreOutOfBoundsException ('Beware hero! Thou shalt not grow too mighty.') 
      self.stats [which] = amount 

     @property 
     def total (self): return sum (self.stats.values()) 

     def __repr__ (self): 
      return '{}\n{}'.format (self.name, '\n'.join ('{}{:>4}'.format (which, self.stats [which]) for which in ['STR', 'DEX', 'WIS', 'INT'])) 

a = Character ('Daggeroth') 
a.strength += 9 
a.dexterity += 9 
a.wisdom += 5 
a.intelligence += 3 
print (a) 
1

在这种情况下,我宁愿重视可读性而不是效率,因为您可能不会遇到任何性能问题与stat查找。我会说字典看起来更好,因为

attributes['strength'] += 1 

似乎更多地组织我。

0

Python中的字典是用哈希表实现的,所以效率在这里不是问题。使用属性字典更好,因为它更灵活。

例如,如果你想多个字符,那么你可以简单地有属性词典列表。在这种情况下使用变量是不可维护的(您需要类似player1Health, player2Health, ...)。

1

要回答自己的问题相关,在字典中的Python店的变量,因此不会做出非常明显的不同在程序的运行是否将它们存储在字典或简单地作为变量条件。

内置功能全局()和locals()返回的字典存储组变量是那些职能的各个名称所暗示的。

存储这些各种各样的Python程序变量将构造一个类的典型方式。我猜想,在列表和字典之后的某些时候,这些类会被覆盖,所以这可能会比这本书稍微早一些。

这是构建一个类,将看起来像,但:

class Character(object): 
     def __init__(self): 
      self.charisma = 0 
      self.dexterity = 0 
      self.wisdom = 0 
      self.health = 0 
      self.pool = 30 

这有几个好处,而且是一个很容易看到的是,它可以让您轻松地创建多个字符。

alice = Character() 
    bob = Character() 

爱丽丝和鲍勃都是用自己的与它们相关的变量副本来发起的。 Python还将类变量存储在字典中。所以alice.charisma + = 1对bob.charisma没有任何影响。

爱丽丝.__ dict__将包含Alice的每一个这些变量的副本的字典。

0

我认为使用变量将是更有效的方法来解决这个问题。观察:

    print("""" 
          1. strength \n 
          2 Health\n 
         3. wisdom\n 
         4. dexterity 
         5. remove 1 strength for point 
         6. remove 1 health for point 
         7. Remove 1 wisdom for point 
         8. remove 1 dexterity for point 
         """) 

         points=30 
         strength=0 
         health=0 
         wisdom=0 
         dexterity=0 
         default=1 

         while default: 

         choice=int(input("Select attributes that you want:")) 
          if choice==1: 
          strength+=1 
          points-=1 
          print("Strength: ", strength, "\nHealth: ", health, "\nWisdom: ", wisdom, "\nDexterity: ") 

          elif choice==2: 
          health+=1 
          points-=1 
          print("Strength: ", strength, "\nHealth: ", health, "\nWisdom: ", wisdom, "\nDexterity: ", dexterity) 

          elif choice==3: 
          wisdom+=1 
          points-=1 
          print("Strength: ", strength, "\nHealth: ", health, "\nWisdom: ", wisdom, "\nDexterity: ", dexterity) 

          elif choice==4: 
          dexterity+=1 
          points-=1 
          print("Strength: ", strength, "\nHealth: ", health, "\nWisdom: ", wisdom, "\nDexterity: ", dexterity) 

          elif oc==5: 
          strength-=1 
          points+=1 
          print("Strength: ", strength, "\nHealth: ", health, "\nWisdom: ", wisdom, "\nDexterity: ", dexterity) 

         elif oc==6: 
          health-=1 
          points+=1 
          print("Strength: ", strength, "\nHealth: ", health, "\nWisdom: ", wisdom, "\nDexterity: ", dexterity) 

         elif oc==7: 
          wisdowm-=1 
          points+=1 
          print("Strength: ", strength, "\nHealth: ", health, "\nWisdom: ", wisdom, "\nDexterity: ", dexterity) 

         elif oc==8: 
         dexterity-=1 
         points+=1 
         print("Strength: ", strength, "\nHealth: ", health, "\nWisdom: ", wisdom, "\nDexterity: ", dexterity) 

当然,你也可以使用字典,但变量的想法更容易,编程是关于效率。