Defining a variable after calling it?
Question: I've run into a problem in terms of assigning objects. I want to be able to
assign the object via a function, but because the variable `name` is not
defined until during the function, when I first call the function (with name =
john) it says: `NameError: name 'john' is not defined`.

I'm using this script to assist me while I run a game session, and I want to
be able to create new NPC's at will, but is there anyway to do that without
first using: `>>> blahblah = Character()` in a python prompt?

Sorry for the block of code and I'm sorry if there is a simple solution.

    
    
    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]
    

EDIT:EDIT:After calling the function with create_npc('john', 'John Smithson
II', 2, True)

Once I have assigned the Class obect as `'john'` rather than `john`, I don't
know how to access it. For instance: `>>> john.char_name` will give me this
error `NameError: name 'john' is not defined`, and `>>> 'john'.char_name`
gives me `AttributeError: 'str' object has no attribute 'char_name'`


Answer: You don't need to pass name to the function, remove that param from the
function definition and your code should work fine.

