调用后定义一个变量?

时间:2014-05-16 04:18:18

标签: python variables object

我在分配对象方面遇到了问题。我希望能够通过函数分配对象,但因为直到函数期间才定义变量name,所以当我第一次调用函数(name = john)时,它说:     NameError: name 'john' is not defined

我在运行游戏会话时使用此脚本来帮助我,我希望能够随意创建新的NPC,但无论如何都要在没有首先使用的情况下执行此操作: >>> blahblah = Character() 在python提示符中?

对于代码块感到抱歉,如果有简单的解决方案,我很抱歉。

import random
import math

combat_participants = []
npc_list = []

class Character():
    """Basic class for character statistics
    """
    def get_stats(self):
        """Refreshes the stats after new assignment
        """
        self.hp = self.st
        self.will = self.iq
        self.per = self.iq
        self.fp = self.ht
        self.bspeed = (self.ht + self.dx) / 4
        self.dodge = math.floor(self.bspeed + 3)
        self.bmove = math.floor(self.bspeed)
        self.blift = round(self.st*self.st/5)
    def get_status(self):
        """Checks the status of the character
        """
        if self.hp < -self.hp:
                self.status = 'Dead'
        elif self.hp < 0:
                self.status = 'Unconscious'
        else:
                self.status = 'Alive'
    #Primary Attributes
    st = 10
    dx = 10
    iq = 10
    ht = 10
    #Secondary Attributes
    hp = st
    will = iq
    per = iq
    fp = ht
    bspeed = (ht + dx) / 4
    dodge = math.floor(bspeed + 3)
    bmove = math.floor(bspeed)
    blift = round(st*st/5)
    #Other
    status = 'Alive'
    weapon = 'None'
    shield = 'None'
    char_name = ''

def create_npc(name, str_name, level, combat):
    """
    /long and irrelevant/
    """
    name = Character()
    name.char_name = str_name

    #Randomly assigning attributes
    prim_attr_list = [name.st, name.dx, name.iq, name.ht]
    for attr in prim_attr_list:
        attr = random.randint(7+level,9+level)
    name.get_stats(name)

    #Randomly assigns a weapon
    temp_weapon_list = []
    for weapon in weapon_list:
        if weapon.st <= name.st:
    temp_weapon_list += weapon
    name.weapon = temp_weapon_list[random.randint(0,len(temp_weapon_list)-1)]).wep_name

    #Adds them to the npc list
    global npc_list
    npc_list += [name]

    #Adds them to combat list if they are in combat
    global combat_participants
    if combat:
        combat_participants += [name]

编辑:编辑:用create_npc调用函数后('john','John Smithson II',2,True)

我将类对象分配为'john'而不是john后,我不知道如何访问它。例如:>>> john.char_name会给我这个错误NameError: name 'john' is not defined>>> 'john'.char_name会给我AttributeError: 'str' object has no attribute 'char_name'

2 个答案:

答案 0 :(得分:0)

您不需要将名称传递给函数,从函数定义中删除该参数,您的代码应该可以正常工作。

答案 1 :(得分:0)

您要做的是在__init__()课程中添加Character方法。以下是简化的Character类:

class Weapon:

    def __init__(self, name, damage):
        self.name = name
        self.damage = damage
        self.level = 1

    def upgrade(self):
        self.level += 1
        self.damage += 10

class Character:

    def __init__(self, name, level, weapon):
        self.name = name
        self.level = level
        self.weapon = weapon

        self.max_hp = self.get_max_hp()
        self.hp = max_hp

    def get_max_hp(self):
        return 100 + self.level*10

    def get_status(self):
        if self.hp <= 0:
            return 'Dead'
        elif self.hp < self.max_hp:
            return 'Unhealthy'
        else:
            return 'Healthy'

    def level_up(self):
        self.level += 1
        self.max_hp = self.get_max_hp()
        self.weapon.upgrade()

    def attack(self, npc):
        npc.hp -= self.weapon.damage

>>> sword = Weapon('sword', 15)
>>> spear = Weapon('spear', 20)
>>> npc1 = Character('john', 5, sword)
>>> npc2 = Character('james', 1, spear)
>>> npc1.name
'john'
>>> npc1.level
5
>>> npc1.max_hp
150
>>> npc1.weapon.name
'sword'
>>> npc1.weapon.damage
15
>>> npc1.weapon.upgrade()
>>> npc1.weapon.damage
25
>>> npc1.hp
150
>>> npc1.get_status()
'Healthy'
>>> npc2.attack(npc1)
>>> npc1.hp
130
>>> npc1.get_status()
'Unhealthy'
>>> for i in range(10):
        npc2.attack(npc1)
>>> npc1.hp
-70
>>> npc1.get_status()
'Dead'
>>> npc1.level_up()
>>> npc1.level
6
>>> npc1.weapon.damage
35
>>> npc1.max_hp
160
相关问题