Python Forum
function method problems - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: function method problems (/thread-14666.html)



function method problems - drchar - Dec-11-2018

Hi all! Just a quick one as I'm new to Python and have square eyes after attempting to solve a simple issue after working with a java proj. of mine. Just wanted a second opinion.
def ClassSet(x):
    if x == 1:
        bHitPoints = archerClass.hP  
        bDmg = archerClass.Dmg
        bBlockChance = archerClass.bChance
        bBlockAmount = archerClass.DmgbPercent
    elif x == 2:
        bHitPoints = knightClass.hP 
        bDmg = knightClass.Dmg
        bBlockChance = knightClass.bChance
        bBlockAmount = knightClass.DmgbPercent
    elif x == 3:
        bHitPoints = mageClass.hP  
        bDmg = mageClass.Dmg
        bBlockChance = mageClass.bChance
        bBlockAmount = mageClass.DmgbPercent
    return bDmg, bHitPoints, bBlockChance, bBlockAmount
from GameMod.ClassChanges import ClassSet, UpWeapon
from GameMod.ClassesAndMobs import archerClass, knightClass, mageClass
from GameMod.Scenes import Scene1
from GameMod.weaponsNdefense import Bow, c, Sword, Wand, r, Shield, Robe, Armor


global Dmg
global HitPoints
global bChance
global bAmountPerc

def ChangeValues(w,x,y,z):
    Dmg = w
    HitPoints = x
    bChance = y
    bAmountPerc = z

Shield(c)
Robe(c)
Armor(c)
Bow(c)
Sword(c)
Wand(c)
archerClass()
knightClass()
mageClass()  

intro = open("Intro.txt")        
print(intro.read())
#THIS WILL BE SUBSET SOON!
classChoice = int(input("1. Archer \n2. Knight \n3. Mage"))
ChangeValues(ClassSet(classChoice)[0],ClassSet(classChoice)[1], \
             ClassSet(classChoice)[2],ClassSet(classChoice)[3])
#SCENES
#Set the wand to rare level if mage chosen.
UpWeapon(classChoice,Scene1(HitPoints)[1],r,2)
ChangeValues(ClassSet(classChoice)[0],ClassSet(classChoice)[1], \
             ClassSet(classChoice)[2],ClassSet(classChoice)[3])
This is my main class & the only associated function "ClassSet", I'm having issues with setting a variable-set via a main func. I'm not sure why it is, you can see the simple ChangeValues() function, it should just take the global variables and apply the given parameters to it (as shown further down the code). However it doesn't like it. Returning null & throwing exception. Is there any other solution to prevent me repeating 4 lines of code over and over again in replacement of the func. "ChangeValues".

Below you will see a working solution, it's pretty easy to see why I'd want to turn the value-change into a function!
global Dmg
global HitPoints
global bChance
global bAmountPerc
'''
def ChangeValues(w,x,y,z):
    Dmg = w
    HitPoints = x
    bChance = y
    bAmountPerc = z
'''

Shield(c)
Robe(c)
Armor(c)
Bow(c)
Sword(c)
Wand(c)
archerClass()
knightClass()
mageClass()  

intro = open("Intro.txt")        
print(intro.read())
classChoice = int(input("1. Archer \n2. Knight \n3. Mage"))
Dmg = ClassSet(classChoice)[0]
HitPoints = ClassSet(classChoice)[1]
bChance = ClassSet(classChoice)[2]
bAmountPerc = ClassSet(classChoice)[3]
'''ChangeValues(ClassSet(classChoice)[0],ClassSet(classChoice)[1], \
             ClassSet(classChoice)[2],ClassSet(classChoice)[3])'''
Thanks in advance,

Drchar

My mistake! I forgot you have to edit the global copy of the variable! How stupid of me.

RESOLVED FUNCTION
def ChangeValues(w,x,y,z):
    global Dmg
    global HitPoints
    global bChance
    global bAmountPerc
    Dmg = w
    HitPoints = x
    bChance = y
    bAmountPerc = z



RE: function method problems - ichabod801 - Dec-11-2018

Please don't use globals. You want to be clear about what is going on in your functions. You do that with parameters and return values. See the function tutorial on how to do that. For something like this I would say a class for the player would work best:

class Player(object):

    def __init__(self, classIndex):
        if x == 1:
            self.HitPoints = archerClass.hP  
            self.Dmg = archerClass.Dmg
            self.BlockChance = archerClass.bChance
            self.BlockAmount = archerClass.DmgbPercent
        elif x == 2:
            self.HitPoints = knightClass.hP 
            self.Dmg = knightClass.Dmg
            self.BlockChance = knightClass.bChance
            self.BlockAmount = knightClass.DmgbPercent
        elif x == 3:
            self.HitPoints = mageClass.hP  
            self.Dmg = mageClass.Dmg
            self.BlockChance = mageClass.bChance
            self.BlockAmount = mageClass.DmgbPercent

intro = open("Intro.txt")        
print(intro.read())
classChoice = int(input("1. Archer \n2. Knight \n3. Mage"))
player = Player(classChoice)
It looks like you might have classes for archer, knight, and mage. If so it might work even better to have a factory function that creates an instance of one of those classes as the player.


RE: function method problems - buran - Dec-11-2018

at least you can refactor the __init__ as

    def __init__(self, classIndex):
        player_classes = {1:archerClass, 2:kingClass, 3:mageClass}
        player_class = playerClasses.get(classIndex)
        self.HitPoints = player_class.hP  
        self.Dmg = player_class.Dmg
        self.BlockChance = player_class.bChance
        self.BlockAmount =player_class.DmgbPercent