0

This is a portion of some code i'm working on and i would like two know how too add two variable together that are from two different functions?

def Dwarf():
    Name = "Dwarf "
    Health = 150
    Mana = 10
    Stamina = 15
    Base_Attack = 20
    Melee_Attack = 0
    Elemental_Attack = 5
    print("You have choosen Dwarf! \n ")
return

def Mage():
    Name = "Mage "
    Health = 10
    Mana = 20
    Stamina = 5
    Base_Attack = 5
    Melee_Attack = 0
    Elemental_Attack = 20
    print("Your Class is Mage! \n ")
return

how do i add the names together and the health/mana ect, to make the race-class character? and be able to call upon it when i need to?

2
  • You code is not valid Python syntax, it's a wannabe-Ruby one. I cannot really tell what you would like to do. I suggest reading the great Python introductory tutorial you will find here. Commented Jan 25, 2014 at 15:53
  • @StefanoSanfilippo: the return command is part of the function body and must be indented. Without any argument it returns the value named None. You get no other value from inside the function. Commented Jan 25, 2014 at 18:03

2 Answers 2

1

Doing this with functions doesn't seem logical. It seems like you need to use classes instead. A class can have attributes like (in your case):

  • name
  • health
  • mana
  • stamina
  • ...

and methods, for example:

  • getName()
  • setName()
  • setMana()
  • ...

A class Dwarf, would look like this:

class Dwarf(object):
    def __init__(self):
        self.name = "Dwarf"
        self.health = 150
        # more attributes

    def getName(self):
        return self.name

    # more methods

something similar with the Mage class. Then you can get the name as:

dwarf1 = Dwarf() print dwarf1.getName()

Of course I would recommend you to read about classes in Python.

You can also pass the attributes values as arguments, so you set them at the moment of creating:

A class Dwarf, would look like this:

class Dwarf(object):
    def __init__(self, name, health):
        self.name = name
        self.health = health
        # more attributes

    def getName(self):
        return self.name

    # more methods

and create an instance:

dwarf2 = Dwarf("strangeNameHere", 200)

Note:

I would recommend you to follow Python naming conventions.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Christian, i was doing this with my friend and as you probably guessed we're pretty new to it and i though using the functions like that would be easiest because i though i can call them and certain things when i need t get information from them and add them together for the characters stats ect.. but thank you so much :)
0

To add to Chistians good advice, it is not neccessary to use getters and setters in simple cases like the self.name. Actually, you just want to read it and assign it. After creating the instance, you can simply access the internal data member as a normal variable. What is named self inside the definition, takes the name of the instance outside. The def getName(): can be comletely removed:

dwarf1 = Dwarf('Bob', 200)
print(dwarf1.name,dwarf1.health)

# Changing the health.
dwarf1.health = 150
print(dwarf1.name,dwarf1.health)

The getters and setters should only be used when you need to do something more than simply reading the value or assigning the value (say some checking or having read only variable). But in such case, you should use Python properties or via the property() function, or possibly more readable the @property decorator -- see http://docs.python.org/3.3/library/functions.html#property

Actually, it seem that you want to share the same set of properties for both Dwarf and Mage -- only the values differ. You can distinguish the class by the class name passed when the object is created like this:

class Being:

    def __init__(self, clsname, name):
        self.name = name
        self.clsname = clsname

        if self.clsname == 'dwarf':
            self.health = 150
            self.mana = 10
            self.stamina = 15
            self.base_attack = 20
            self.melee_attack = 0
            self.elemental_attack = 5

        elif self.clsname == 'mage':
            self.health = 10
            self.mana = 20
            self.stamina = 5
            self.base_attack = 5
            self.melee_attack = 0
            self.elemental_attack = 20

Notice that self.name is distinguished from self.clsname. Now you can create dwarfs and mages like this:

dw1 = Being('dwarf', 'Bob')
dw2 = Being('dwarf', 'John')
mag1 = Being('mage', 'Merlin')
mag2 = Being('mage', 'Gandalf')

After some trial and failures you will find that it is really easier to define a base class Being and the derived classes Dwarf and Mage...

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.