0

I'm still learning and like to build things that I will eventually be doing on a regular basis in the future, to give me a better understanding on how x does this or y does that.

I haven't learned much about how classes work entirely yet, but I set up a call that will go through multiple classes.

getattr(monster, monster_class.str().lower())(1)

Which calls this:

class monster:
def vampire(x):
    monster_loot = {'Gold':75, 'Sword':50.3, 'Good Sword':40.5, 'Blood':100.0, 'Ore':.05}
    if x == 1:
        loot_table.all_loot(monster_loot)

Which in turn calls this...

class loot_table:
def all_loot(monster_loot):
    loot = ['Gold', 'Sword', 'Good Sword', 'Ore']
    loot_dropped = {}

    for i in monster_loot:
        if i in loot:
            loot_dropped[i] = monster_loot[i]

    drop_chance.chance(loot_dropped)

And then, finally, gets to the last class.

class drop_chance:
def chance(loot_list):

    loot_gained = []

    for i in loot_list:
        x = random.uniform(0.0,100.0)

        if loot_list[i] >= x:
            loot_gained.append(i)

    return loot_gained

And it all works, except it's not returning loot_gained. I'm assuming it's just being returned to the loot_table class and I have no idea how to bypass it all the way back down to the first line posted. Could I get some insight?

2 Answers 2

1

Keep using return.

def foo():
  return bar()

def bar():
  return baz()

def baz():
  return 42

print foo()
Sign up to request clarification or add additional context in comments.

4 Comments

Will I have to define loot_gained in each class definition it's using then?
Uh, no... you just need to keep using return.
I'm just beginning to confuse myself with all the coding I've done to that... sorry about that. I forgot to set getattr to print, and did as you suggested as well. But, I'm getting a return of the location of loot_table. "<__main__.loot_table object at 0x000000000340B3C8>"
Then you have another bug in your code unrelated to this problem.
0

I haven't learned much about how classes work entirely yet...

Rather informally, a class definition is a description of the object of that class (a.k.a. instance of the class) that is to be created in future. The class definition contains the code (definitions of the methods). The object (the class instance) basically contains the data. The method is a kind of function that can take arguments and that is capable to manipulate the object's data.

This way, classes should represent the behaviour of the real-world objects, the class instances simulate existence of the real-world objects. The methods represent actions that the object apply on themselves.

From that point of view, a class identifier should be a noun that describes category of objects of the class. A class instance identifier should also be a noun that names the object. A method identifier is usually a verb that describes the action.

In your case, at least the class drop_chance: is suspicious at least because of naming it this way.

If you want to print something reasonable about the object--say using the print(monster)--then define the __str__() method of the class -- see the doc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.