1

I setup a class and it accepts and prints out the variables fine in one if statement.

class npc: #class for creating mooks
    def __init__(self, name):
        self.name = name
    def npc_iq (self,iq):
        self.iq = []
    def npc_pp (self,pp):
        self.pp = []
    def npc_melee (self, melee):
        self.melee = []
    def npc_ct (self, ct):
        self.ct = []

It works fine in this if statement

if menu_option == 1:
    print "Choose melees for npc"
    init_bonus = random.randint(0,2)
    char_PP = random.randint(7,15)
    char_iq = random.randint(7,15)
    npc_Melees = int(raw_input(prompt))
    combat_time = math.floor((round_attacks - init_bonus - math.floor(char_PP/2) - math.floor(char_iq/2)) / npc_Melees)
    #function for calculating sequence number
    print "combat time is"
    print combat_time
    mook = "mook%s" % counter # adds different mook names to program
    mook = npc(mook) 
    mook.iq = (char_iq)
    mook.pp = (char_PP)
    mook.melee = (npc_Melees)
    mook.ct = (combat_time)
    counter += 1

But on this statement it will print out the name in the class but not ct.

elif menu_option ==4:
    print "Printing out all mooks"
    print
    printcount = counter -1
    while printcount != 0:
        mookprint = "mook%s" % printcount
        mookprint = npc(mookprint)
        print mookprint.name
        print mookprint.ct 
        print
        printcount -= 1
Wilduck
  • 13,822
  • 10
  • 58
  • 90
  • 2
    If it prints the `name` then `mookprint` is not `None`. So are you sure `ct` actually has a value? – Simeon Visser Mar 15 '12 at 21:43
  • The value of ct prints fine in the first if loop. I get the error of AttributeError: npc instance has no attribute 'ct' when I run the other line. – user1267762 Mar 15 '12 at 21:44
  • 4
    You seem to have some misunderstanding about how classes work. For example, you are printing moodprint.ct without ever having set the ct member of the object. – Vaughn Cato Mar 15 '12 at 21:47

4 Answers4

3

Why would a mookprint have any idea what value ct should be? The constructor for npc initialises a new instance of npc, with the name given as a parameter, but ct is left empty.

When you create an NPC in menu option 1, you do not create a global instance of npc. If you want to refer to a previously created instance of npc, you will need to find some way of storing them. Dictionaries may be a good solution for you. A dictionary is an object that holds mappings between keys and values. If you know the key, then you can find the assosicated value. In this case you would make name the key and the value the npc instances.

eg.

npcsDict = dict()

if menu_option == 1:

   # code for intialising a new instance of npc
   ...
   # most, if not all of the initialisation code should be moved to the 
   # __init__ method for npc

   # now store the newly created mook
   npcsDict[mook.name] = mook

elif menu_option == 4:

    print "Printing out all mooks"
    print
    for mookName in npcsDict:
        print npcsDict[mookName].name
        print npcsDict[mookName].ct
        print
Dunes
  • 37,291
  • 7
  • 81
  • 97
0

The elif will only be executed if the if has not, so if the elif block runs, ct was never set

Intra
  • 2,089
  • 3
  • 19
  • 23
0

i dont really understand your problem.

your working example:

mook = npc(mook) 
mook.iq = (char_iq)
mook.pp = (char_PP)
mook.melee = (npc_Melees)
mook.ct = (combat_time)

mook.ct is value of (combat_time)

your failing example:

mookprint = npc(mookprint)
print mookprint.name
print mookprint.ct 

mookprint.ct's value is nothing because it is never set.

mo.
  • 3,474
  • 1
  • 23
  • 20
0

I don't think you're understanding how four lines work:

mookprint = "mook%s" % printcount
mookprint = npc(mookprint)
print mookprint.name
print mookprint.ct 

Every time this block of code is run, the following things are happending:

  1. You're assigning a string of the form "mook" to the variable mookprint
  2. You're creating a new instance of the npc class. You should note that all of the instances you're creating will be separate from eachother. This new instance will have an attribute with the name that was previously held in the variable mookprint and this instance of npc will be assigned to mookprint.
  3. You're printing the name attribute of the instance of the npc class that you created in the previous step. This works because when this instance was created, the __init__ method of your class was called with the argument name being set to "mook1" or whatever was stored in mookprint at the time.
  4. You're printing the ct attribute of the instance of the npc class that you just created. Since you never set the ct attribute to anything, this will not work how you expected.

If you want to count the number of instances of your npc class, you'll need to create a class attribute. This is a variable whose value is common across all instances of a class. To do so, you'll need to modify your class definition to add an item to this attribute every time you make a new instance of the class. It will look something like this:

class npc: #class for creating mooks
    ct = []
    def __init__(self, name):
        self.name = name
        self.ct.append(name)

    def get_ct(self):
        return len(self.ct)

With the above, the variable ct will be a list that is common to all instances of npc and will grow every time a new npc is created. Then the method get_ct will count how long this list is.

Then you'll need to modify the four lines I mentioned to look like:

mookprint = "mook%s" % printcount
mookprint = npc(mookprint)
print mookprint.name
print mookprint.get_ct()

I think the code above shows how to change your code to work more how you expected it to work. However, it should be noted that you rarely want to create classes where each instance depends on information about the other instances. It is usually a better design to do something like Dunes suggested, storing the instances in a dictionary, or some other data structure, and keeping track of them that way.

Community
  • 1
  • 1
Wilduck
  • 13,822
  • 10
  • 58
  • 90