字典与变量 [英] Dictionary vs Variable

查看:40
本文介绍了字典与变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在阅读绝对入门的Python编程第三版".挑战之一是:

I am currently reading "Python Programming for the Absolute Beginning 3rd Edition". One of the challenges is:

编写一个用于角色扮演游戏的Character Creator程序.这应该给玩家30分的积分池,让他们花在4点上属性:力量,健康,智慧和敏捷.这玩家应该能够在任何情况下从池中花费积分属性,并且还应该能够从属性,然后将它们放回池中.

Write a Character Creator program for a role-playing game. The player should be given a pool of 30 points to spend on four attributes: Strength, Health, Wisdom, and Dexterity. The player should be able to spend points from the pool on any attribute and should also be able to take points from an attribute and put them back into the pool.

起初我是用变量写的

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

本节是关于列表和词典的.所以我的问题是:以这种方式使用变量是否更好?还是可以使用字典?如果是这样,效率更高吗?即:

This section is regarding lists and dictionaries. So my question is: Is it better to use the variables in this manner or is it possible to use dictionaries? If so, is it more efficient? ie:

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

推荐答案

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

To make it short: I would go for the dictionary.

要长篇大论:这可能是直接研究面向对象编程的好例子.

To make it long: This could be a nice example to delve directly into object oriented programming.

#! /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)

这篇关于字典与变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆