How can I call two variables of two different functions and add them? (Python3)

advertisements

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?


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.